1.内存层面需要准对多个数据进行存储,此时,可以考虑的容器有:数组,集合类 2.数组存储多个数据方面的特点: >数组一旦初始化,长度确定。 >数组中多个元素是一次紧密排列,有序的,可重复的 >(优点)数组一旦初始化完成,其元素的类型就是确定的。不是次类型的元素,就不能添加到此数组中。 int[] arr = new int[10]; arr[0] = 1; arr[1] = "AA"; Object[] arr1 = new Object[10]; arr1[0] = new String(); arr1[1] = new Date(); >(优点)元素的类型既可以是基本数据类型,也可以是引用数据类型。 数组存储多个数据方面的弊端: >数组一旦初始化,其长度就不可变了 >数组中存储数据特点的单一性,对于无序的,不可重复的场景的多个数据就无能为力了。 >数组可用的方法,属性都极少。具体的需求需要自己组织相关的代码逻辑。 >针对于数组中元素的删除、插入操作,性能较差。 3.java集合框架体系 java.util.Conllection:存储一个一个的数据 -----子接口:List:存储有序的,可重复的数据("动态"数组) -------ArrayList(主要实现类)、LinkedList、Vector -----子接口: Set: 存储无序的、不可重复的数据(高中的集合:确定性,无序性,互异性) ------- HashSet、LinkedHashSet、TreeSet java.util.Map:存储一对一对的数据(key-value键值对,(x1,y1)、(x2,y2) -->y = f(x),类似高中的函数) HashMap、LinkedHashMap、TreeMap、Hashtable、Properties 4.学习的程度把握: 层次1:针对具体特点的多个数据,知道相应的适合的接口的主要实现类,会实例化,会调用的方法。 层次2:区分接口中不同的实现类的区别。 *************** 层次3:①针对常用的实现类,需要熟悉底层的源码 ②底层源码 ③常用数据结构
package cd_one.code15;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionTest {
@Test
public void test() {
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add(123);//自动装箱
coll.add("尚硅谷");
coll.add(new Object());
coll.add(new Person("Tom", 12));
System.out.println(coll);
// [AA, 123, 尚硅谷, java.lang.Object@491cc5c9, Person{name='Tom', age=12}]
// addAll(Collection other)
Collection coll1 = new ArrayList();
coll1.add("BB");
coll.add(456);
coll.addAll(coll1);
//[AA, 123, 尚硅谷, java.lang.Object@491cc5c9, Person{name='Tom', age=12}, 456, BB]
coll.add(coll1);
// [AA, 123, 尚硅谷, java.lang.Object@491cc5c9, Person{name='Tom', age=12}, 456, BB, [BB]]
System.out.println(coll);
System.out.println();
// size():
System.out.println(coll.size());//7
}
@Test
public void test2(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
coll.add(new Person("Tom", 12));
Person p1 = new Person("Tom",12);
coll.add(p1);
//isEmpty()
System.out.println(coll.isEmpty());//false
System.out.println(coll.contains("AA"));//true
System.out.println(coll.contains(123));//ture
System.out.println(coll.contains(128));//false
System.out.println(coll.contains(new String("尚硅谷")));//true
System.out.println(coll.contains(p1));//true,因为比较的是地址
System.out.println(coll.contains(new Person("Tom", 12)));//false 因为是new了一个新对象,和上面不一样了
//重写equal方法后变成了true,因为比较的不再是==
Collection coll1 = new ArrayList();//多态
// //add()
// coll1.add("AA");
// coll1.add("BB");
// System.out.println(coll.containsAll(coll1));//false
coll1.add("AA");
coll1.add(123);
//containAll()
System.out.println(coll.containsAll(coll1));//true
System.out.println(coll.retainAll(coll1));
}
@Test
public void test3(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add("AA");
Person p1 = new Person("Tom",12);
coll.add(p1);
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
System.out.println(coll);
// coll.clear();
// System.out.println(coll); //[]
// System.out.println(coll.size());//0
// coll.remove(p1); //删掉了
coll.remove(new Person("Tom",12));//也删掉了,因为已经重写过了
coll.remove("AA");//找到其中一个AA就停止
System.out.println(coll);//[AA, 123, 尚硅谷]
}
}
@Test
public void test4(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add("AA");
Person p1 = new Person("Tom",12);
coll.add(p1);
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
//集合--->转化为数组
Object[] arr = coll.toArray();
System.out.println(Arrays.toString(arr));
//[AA, AA, Person{name='Tom', age=12}, 123, 尚硅谷]
System.out.println(coll.hashCode());
}
@Test
public void test5(){
String[] arr = new String[]{"AA","BB","CC"};
Collection list = Arrays.asList(arr);
//数组转集合
System.out.println(list);
//[AA, BB, CC]
List list1 = Arrays.asList("AA", "BB", "CC", "DD");
System.out.println(list1);
//[AA, BB, CC, DD]
}
@Test
public void test6(){
Integer[] arr = new Integer[]{1,2,3};//自动装箱:基本数据类型转化为包装类
//数组转集合:调用的事Arrays的静态方法aList(object ... objs),因为上面是三个对象,他转化为包装类了
List list1 = Arrays.asList(arr);
System.out.println(list1.size());//3
int[] arr1 = new int[]{1,2,3};//这个是因为是基本数据类型,基本数据类型只能看成整个数组,数组可以看成一个对象,所以可以放进来,
// 所以是1
List list2 = Arrays.asList(arr1);
System.out.println(list2.size());//1,都在一个集合里面,所以是一次
}
package cd_one.code15;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorTest {
@Test
public void test1(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add("AA");
Person p1 = new Person("Tom",12);
coll.add(p1);
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
//获取迭代器对象
Iterator iterator = coll.iterator();
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// AA
//AA
// Person{name='Tom', age=12}
// 123
// 尚硅谷
// System.out.println(iterator.next());
//NoSuchElementException 没有这个元素的异常
//方式2:
// for (int i = 0; i < coll.size(); i++) {
// System.out.println(iterator.next());
// }
//方式3:
while(iterator.hasNext()){//判断是否还有下一个元素
System.out.println(iterator.next());
}
}
@Test
public void test2(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add("BB");
Person p1 = new Person("Tom",12);
coll.add(p1);
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
//错误遍历:
// while ((iterator.next()) != null) {
// System.out.println(iterator.next());
// }
//错误:每次调用coll.iterator(),都会返回一个新的迭代器对象
// while(coll.iterator().hasNext()){//判断是否还有下一个元素
// System.out.println(coll.iterator().next());//一直循环AA
// }
}
}
package cd_one.code15;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class ForTest {
@Test
public void test(){
Collection coll = new ArrayList();//多态
//add()
coll.add("AA");
coll.add("AA");
Person p1 = new Person("Tom",12);
coll.add(p1);
coll.add(123);//自动装箱
coll.add(new String("尚硅谷"));
//内部元素类型 临时变量: 容器
for (Object obj : coll){
System.out.println(obj);
}
// AA
// AA
// Person{name='Tom', age=12}
// 123
// 尚硅谷
}
@Test
public void test2(){
int[] arr = new int[]{1,2,3,4,5};
//元素类型 临时变量 :容器
for (int i : arr){
System.out.println(i);
}
}
@Test
public void test3(){
String[] arr = new String[]{"AA","BB","CC","DD"};
for (String s : arr){
System.out.println(s);
}
}
}
因为增加for循环是赋值地址,输出还是原本的那个,就算改了,也是新弄了一个新的变量,跟这个一点关系都没有