2019-05-25 Java Learning Diary day15

Posted by tc48 on Sat, 25 May 2019 21:50:36 +0200

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

import 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());
    }

}
case

Set traversal

package tan.jung.array;

import java.util.ArrayList;
import java.util.Collection;

import tan.jung.bean.Student;

public class demo3_Array {

    public static void main(String[] args) {
        Collection s1=new ArrayList();
        s1.add(new Student("War three",20)); //object oj=new Student("",);
        s1.add(new Student("War three",22));
        s1.add(new Student("War three",24));
        s1.add(new Student("War three",26));
        
        Object[] arr =s1.toArray(); //Converting a set to a number
        
        for (int i = 0; i < arr.length; i++) {
            Student s2=(Student)arr[i];//Downward transformation
            System.out.println(s2.getName()+","+s2.getAge());
        }

    }

}
Exercises

 

Collection with All functionality

  boolean addAll(Collection c)

  boolean removeAll(Collection c)

  boolean containsAll(Collection c)

  boolean retainAll(Collection c)

import java.util.ArrayList;
import java.util.Collection;

public class demo4_Collection {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {
        //demo1();
        //demo2();
        //demo3();
        Collection c1=new ArrayList();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        
        Collection c2=new ArrayList();
        c2.add("a");
        c2.add("b");
        c2.add("c");
        c2.add("d");
        c2.add("z");
        
        //If the collection of calls changes, it returns true,If the collection of calls remains unchanged, it returns false
        boolean b=c1.retainAll(c2); //intersect
        System.out.println(b);
        System.out.println(c1);
    }

    public static void demo3() {
        Collection c1=new ArrayList();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        
        Collection c2=new ArrayList();
        c2.add("a");
        c2.add("b");
        c2.add("z");
        boolean b=c1.containsAll(c2); //Determine whether the collection invoked contains the incoming collection
        System.out.println(b);
    }

    public static void demo2() {
        Collection c1=new ArrayList();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        
        Collection c2=new ArrayList();
        c2.add("a");
        c2.add("b");
        c2.add("z");

        boolean b=c1.removeAll(c2); //Delete intersection
        System.out.println(b);
        System.out.println(c1);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void demo1() {
        Collection c1=new ArrayList();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        
        Collection c2=new ArrayList(); //alt+shift+r Rename together
        c2.add("a");
        c2.add("b");
        c2.add("c");
        c2.add("d");
        
        //c1.addAll(c2);    //take c2 add to c1 in
        c1.add(c2);         //take c2 Add as an object c1 in
        System.out.println(c1);
    }

}
case

 

iterator

Collections are used to store elements. Stored elements need to be viewed, and then iteration (traversal) is required.

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import tan.jung.bean.Student;

public class demo5_Iterator {

    public static void main(String[] args) {
        //demo1();
        Collection c1=new ArrayList();
        c1.add(new Student("Li Si",20)); //object oj=new Student("",);
        c1.add(new Student("s",20));
        c1.add(new Student("a",20));
        c1.add(new Student("tmenty",20));
        
        Iterator it=c1.iterator();
        while (it.hasNext()) {
            Student s1=(Student)it.next();
            System.out.println(s1.getName()+s1.getAge());
            
        }

    }

    public static void demo1() {
        Collection c1=new ArrayList();
        c1.add("a");
        c1.add("b");
        c1.add("c");
        c1.add("d");
        
        Iterator it=c1.iterator();
        /*boolean b1=it.hasNext(); //Determine whether there are elements in the set, and return true if there are.
        Object obj1=it.next();
        System.out.println(b1);
        System.out.println(obj1);*/
        
        while (it.hasNext()) {
            System.out.println(it.next());        
        }
    }

}
case

Topics: PHP Java Programming