12 foreach 装箱拆箱 可变参数 枚举
来源:互联网 发布:dwg转excel软件 编辑:程序博客网 时间:2024/05/20 19:18
Foreach
int []a=new int[]{1,2,3,4,5,6,7,8};for (int item : a){System.out.println(item);}
1. 当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
自动装箱/拆箱大大方便了基本类型数据和它们包装类的使用。
•自动装箱:基本类型自动转为包装类.(int >> Integer)
•自动拆箱:包装类自动转为基本类型.(Integer >> int)
int a = 3;Collection<Integer> c = new ArrayList<Integer>();c.add(3);//将int类型的3转换为Integer类型并放到集合当中c.add(a + 3);for(Integer i : c){System.out.println(i);}
HashMap<String ,Integer> hashMap=new HashMap<String ,Integer>();for(String itemString :args){hashMap.put(itemString,(null==hashMap.get(itemString)?1:hashMap.get(itemString)+1));}Iterator<Map.Entry<String,Integer>> iterator=hashMap.entrySet().iterator();while (iterator.hasNext()){Map.Entry<String,Integer> entry=(Entry<String,Integer>) iterator.next();System.out.println("单词 "+entry.getKey()+" 出现的次数 :"+entry.getValue());}
2. Integer类有一个缓存,它会缓存介于-128~127之间的整数。
当生成缓冲中的数字,就指向缓存中的数字,不再生成新的Integer对象,所以i1==i2
当超出缓存中的数字,则生成新的Integer对象 所以i3!=i4
Integer i1=100;Integer i2=100;Integer i3=200;Integer i4=200;System.out.println(i1==i2);System.out.println(i3==i4);
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }3. 可变参数:可变参数本质上就是一个数组,对于某个声明了可变参数的方法来说,我们既可以传递离散的值,也可以传递数组对象。但如果将方法中的参数定义为数组,那么只能传递数组对象而不能传递离散的值。
4. 可变参数必须要作为方法参数的最后一个参数,即一个方法不可能具有两个或两个以上的可变参数。
private static int sum(String str, int... nums){System.out.println(str);int sum = 0;for (int num : nums){sum += num;}return sum;}public static void main(String[] args){int result = sum("a", new int[] { 1, 2 });System.out.println(result);result = sum("b", 1, 2, 3, 4);System.out.println(result);}5. 枚举(Enum):我们所定义的每个枚举类型都继承自java.lang.Enum类,枚举中的每个成员默认都是public static final的。
6. 而每个枚举的成员其实就是您定义的枚举类型的一個实例(Instance)。换句话说,当定义了一个枚举类型后,在编译时刻就能确定该枚举类型有几个实例,分别是什么。在运行期间我们无法再使用该枚举类型创建新的实例了,这些实例在编译期间就已经完全确定下来了。
枚举应用实例
public enum AccessRight{MANAGER, DEPARTMENT, EMPLOYEE;}
public class AccessControl{public static boolean checkRight(AccessRight accessRight){if(accessRight == AccessRight.MANAGER){return true;}else if(accessRight == AccessRight.DEPARTMENT){return false;}return false;}public static void main(String[] args){AccessRight accessRight = AccessRight.valueOf("MANAGER");System.out.println(checkRight(accessRight));}}7. 静态导入:
a) import static com.shengsiyuan.common.Common.Age;
b) import static com.shengsiyuan.common.Common.output;
8. 表示导入Common类中的静态成员变量AGE以及静态方法output。注意:使用import static时,要一直导入到类中的静态成员变量或静态方法。
9. Java中,无论生成某个类的多少个对象,这些对象都会对应于同一个Class对象。
0 0
- 12 foreach 装箱拆箱 可变参数 枚举
- 关于静态导入,装箱与拆箱,枚举,以及可变参数的学习
- 黑马程序员--java 静态导入、可变参数、数据类型的自动拆箱与装箱、枚举
- 黑马程序员--枚举,增强for循环,可变参数,静态导入,自动拆箱和自动装箱
- 黑马程序员_高新技术一(高级for,静态导入,可变参数,自动装箱拆箱,枚举)
- 黑马程序员_高新技术1(高级for,静态导入,可变参数,自动装箱拆箱,枚举)
- 1.5新特性静态导入、增强for、可变参数、自动装箱拆箱、枚举
- java基础加强(枚举,反射,增强for循环,可变参数,自动拆箱装箱,内省)
- java-自动装箱/自动拆箱/对象克隆/枚举/参数可变
- java基础增强--自动装箱拆箱、增强for循环、可变参数、枚举类、内省、beanUtils
- java JDK1.5新特性(一) [静态导入] [可变参数] [增强for] [自动装箱拆箱] [枚举]
- 黑马程序员<Java 1.5新特性>静态导入、增强for、可变参数、自动装箱拆箱、枚举
- Java基础加强<二>可变参数、增强for循环、基本类型的自动拆箱与装箱、枚举
- 静态导入 可变参数 增强for循环 自动装箱 枚举
- 黑马程序员_自动装箱、枚举、可变参数、注解、JavaBean
- java(17) - 增强for循环、装箱拆箱、可变参数
- 黑马程序员 高新技术之可变参数与装箱拆箱
- 可变参数与自动装箱拆箱的特例
- Android Test Automation Robotium VS. UIAutomator
- Oracle导入导出
- ListView+CursorAdapter+LoaderManger的配合使用
- 通过PowerShell卸载所有的SharePoint 2010 解决方案
- android 开源项目分类汇总
- 12 foreach 装箱拆箱 可变参数 枚举
- JPA实体注解(三)
- fmpeg 自定义IO中断 AVFormatContext::interrupt_callback ,AVIOInterruptCB具体处理方法
- Eclipse全键盘编码攻略之一——入门
- java.io.filenotfoundexception: the system cannot find the file specified
- C++ do while 0 的用法和意义
- pssh,pscp,pslurp使用实践
- UINavigationController详解(二)
- socket,HTTP,ASIHttpRequest的区别