The Add method in collection:
Code:
public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();//Parent reference to child class object. Here is a collection of lists.
Boolean B1=c.add (New Student ("ZZ", 15));
Boolean B2=c.add (New Student ("ZZ", 15));
System.out.println (B1);
System.out.println (B2);
System.out.println (c);
}
Operation Result:
True
True
[Student [Name=zz, age=15], Student [Name=zz, age=15]]
It is important to note that when you call the Add method of collection, List allows multiple identical data to be added, regardless of what you add, the return value must be true. Unlike set, it does not allow the same data in the collection, and returns flase if the same data exists. Arrylist the parent class of the parent class, overriding the ToString method, and the output object is not the Tostrng method in the object class.
Remove method in Collection:
Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
System.out.println (c);
}
Operation Result:
[A, C, d]
It is clear that the Remove method in call collection can delete the specified element.
The Clear method in collection.
Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
C.clear ();
System.out.println (c);
}
Operation Result:
[]
It is important to note that the collection is emptied, and the Print method returns "[]" by default.
The Contains method in collection.
Code:
public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
C.clear ();
System.out.println (C.contains ("a"));
System.out.println (C.contains ("z"));
System.out.println (c);
}
Operation Result:
True
False
[A, B, C, d]
Call the Contains method to determine whether the collection contains, contained as true, not contained as false.
Collection whether it is empty, isEmpty ().
View the number of elements in collection, size (). There are no examples here.
Basic features of Colllection in Java