1 简单对象数组的构造与使用
1.1 Practice类
package Object;
public class Practice {
public static void main(String[] args){
Student[] students = new Student[5];
Student s1 = new Student("小明", 27);
Student s2 = new Student("小红", 30);
Student s3 = new Student("小强", 30);
Student s4 = new Student("旺财", 12);
Student s5 = new Student("张三", 35);
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
students[4] = s5;
for (int x = 0;x < students.length;x++){
Student s = students[x];
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
1.2 Student.java
package Object;
public class Student {
private String name;
private int age;
public Student(){
super();
}
public Student(String name, int age){
super();
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
@Override
public String toString(){
return "Student [name=" + name + ",age=" + age + "]";
}
}
1.3 关于简单对象数组的内存图解
2 高级对象数组::集合简介
2.1 集合的由来及数组的区别
- 由来:面向对象语言都是以对象的形式, 为方便对多个对象的操作,java提供了集合类
- 区别:
- 数组可以存储同一类型的基本数据也可以存储.同一类型的对象,但长度是固定的
- 集合可以存储不同类型的对象,集合长度可变,可以存储不同类型的对象
2.2 框架的顶层Collection接口
2.3 Collection集合的功能概述
- Collection是层次结构中的根接口。
- Collection 表示一组对象,这些对象也称为 collection 的元素。
- 一些 collection 允许有重复的元素,而另一些则不允许。
- 一些 collection 是有序的,而另一些则是无序的。
- JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。
- 此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
2.4 Collection集合的基本功能测试
2.4.1 成员方法介绍
boolean add(E e)
boolean remove(Object o)
void clear()
boolean contains(Object o)
boolean isEmpty()
int size()
2.4.2 基本功能测试
package Collection_1;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collection;
public class test_collection {
public static void main(String[] args){
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
System.out.println("remove:" + c.remove("hello"));
System.out.println("remove:" + c.remove("javaee"));
System.out.println("contains:" + c.contains("hello"));
System.out.println("contains:" + c.contains("android"));
System.out.println("isEmpty:" + c.isEmpty());
System.out.println("size:" + c.size());
System.out.println("c:" + c);
}
}
2.5 Collection集合的高级功能测试
2.5.1 成员方法介绍
boolean addAll(Collection<? extends E>c):
boolean removeAll(Collection<?> c):
boolean containsAll(Collestion<?> c):
boolean retainAll(Collection<?> c):
2.5.2 基本功能测试
c1.addAll(c2);
c1.removeAll(c2);
c1.containsAll(c2);
c1.retainAll(c2);
还有一些没补充完整,先这样,够写项目了,待补充