aggregate
Overview of object arrays, using
import java.security.acl.Permission; import tan.jung.bean.Student; public class demo1_Array { public static void main(String[] args) { //int arr=new int[5]; //Create basic data type numbers Student [] arr=new Student[5]; //Create reference data type numbers arr[0] = new Student("Zhang San",20);//Create an object and store it in the first place of the number arr[1] = new Student("li Three",19); arr[2] = new Student("Not three",21); arr[3] = new Student("mei",25); arr[4] = new Student("Someone",30); for (int i = 0; i < arr.length; i++) { String s1=String.valueOf(arr[i]); System.out.println(s1); } } } //Second bags package tan.jung.bean; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } 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; } public Student(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
The Origin of Collection
Array length is fixed, when the added elements exceed the length of the array, we need to redefine the array. It's too cumbersome. We have set classes in java.
Can store any object, the length can be changed, with the increase of elements, with the decrease of elements.
Differences between arrays and collections
1: Arrays can store both basic data types and reference data types. Basic data types store values and reference data types store address values.
Collections can only store reference data types (objects) or basic data types in collections.
But when stored, it automatically boxes the programming object 100 new Integer(100)
2: Array length is fixed and state grows automatically
The length of the set is variable and can be increased with the original increase.
When do arrays and collections work
If the number of elements is a fixed recommended array
If the sum of elements is not a fixed recommended set
Collection set
caseimport java.util.ArrayList; import java.util.Collection; import tan.jung.bean.Student; @SuppressWarnings({ "rawtypes", "unchecked" }) public class demo2_Collection { /*add Method always returns true if it is a List collection, because duplicate elements can be stored in the List collection If it's a set set set, it returns flase when it stores duplicate elements ArrayList The parent of the parent class overrides the toString method, so when printing an object, The output is not the result of toString in the Object class*/ public static void main(String[] args) { //demo1(); Collection c1=new ArrayList(); c1.add("a"); c1.add("c"); c1.add("b"); c1.add("d"); c1.remove("a");//delete c1.clear();//empty System.out.println(c1.contains("a"));//Determine whether to include System.out.println(c1.isEmpty());//Judge whether it is empty System.out.println(c1.size());//Get the number of elements System.out.println(c1); } public static void demo1() { Collection c1=new ArrayList();//A parent reference points to a child class object boolean b1=c1.add("woaixuexi"); boolean b2=c1.add(true); boolean b3=c1.add(100); boolean b4=c1.add(new Student("Zhang San",20)); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); System.out.println(c1.toString()); } }