-----------android培训、java培训、java学习型技术博客、期待与您交流! ------------
<一>静态导入
import语句可以导入一个类或某个包中的所有类
import static语句导入一个类中的某个静态方法或所有静态方法
语法举例:
import static java.lang.Math.sin;
import static java.lang.Math.*;
Ex.
public class Demo12 {
public static void main(String[] args) {
System.out.println(Math.max(12, 15));
}
}
使用静态导入的:
package reviewDemo76;
import static java.lang.Math.*;
public class Demo12 {
public static void main(String[] args) {
System.out.println(max(12, 15));
}
}
<二>自动拆箱与装箱
——> -128~127之间的特殊性。为什么要这样设计,好处?——> 享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实例。
public class Demo2 {
public static void main(String[] args) {
List list = new ArrayList();
int i = 10;//Integer
list.add(new Integer(i));
//5.0的做法
int j = 100;
list.add(j);//实际上编译器把基本类型的j自动调用了new Integer(10),传递给集合
//遍历
int k = (Integer)list.get(1);//自动拆箱
System.out.println(k);
//注意的问题
// Integer ii = null;
// int l = ii;
// System.out.println(l);
System.out.println("-------------------------");
Boolean b1 = true;
Boolean b2 = true;
System.out.println(b1==b2);
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1==i2);
Integer i3 = 129;
Integer i4 = 129;
System.out.println(i3==i4);
Float f1 = 0.1f;
Float f2 = 0.1f;
System.out.println(f1==f2);
}
我的总结:对于享元模式,将最常用的封装以便于我们可以复用!
<三>增强for循环
语法:for ( type 变量名:集合变量名 ) { … }
注意事项:
迭代变量必须在( )中定义!
集合变量可以是数组或实现了Iterable接口的集合类
Ex.
//增强for循环
public class Demo3 {
//数组
@Test //ctrl+1
public void test1(){
String s[] = {"a","b","c"};
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
for(String ss:s){
System.out.println(ss);
}
}
@Test
//List
public void test2(){
List l = new ArrayList();
l.add("aa");
l.add("bb");
l.add("cc");
Iterator it = l.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s);
}
for(Object obj:l){
// String s = (String)obj;
// System.out.println(s);
System.out.println(obj);
}
}
//传统方式遍历Map
@Test
public void test3(){
Map map = new LinkedHashMap();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
Set keys = map.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
String key = (String)it.next();
String value = (String) map.get(key);
System.out.println(key+"="+value);
}
}
@Test//增强for遍历map方式一
public void test31(){
Map map = new LinkedHashMap();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
Set keys = map.keySet();
for(Object obj:keys){
String key = (String)obj;
String value = (String) map.get(key);
System.out.println(key+"="+value);
}
}
//传统方式遍历Map
@Test
public void test4(){
Map map = new LinkedHashMap();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
Set me = map.entrySet();
Iterator it = me.iterator();
while(it.hasNext()){
Map.Entry m = (Map.Entry)it.next();
String key = (String) m.getKey();
String value = (String)m.getValue();
System.out.println(key+"="+value);
}
}
@Test//增强for循环遍历map方式二
public void test41(){
Map map = new LinkedHashMap();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
for(Object obj:map.entrySet()){
Map.Entry me = (Map.Entry )obj;
String key = (String) me.getKey();
String value = (String)me.getValue();
System.out.println(key+"="+value);
}
}
@Test//增强for的一些细节
public void test5(){
int arr[] = {1,1,1};
for(int i:arr){
i = 10;
}
//循环时改元素的值必须使用传统方式
// for(int i=0;i<arr.length;i++){
// arr[i] = 100;
// }
System.out.println(arr[0]);
}
@Test
public void test6(){
List list = new ArrayList();
list.add("p");
for(Object obj:list){
obj = "ppp";
}
System.out.println(list.get(0));
}