catalogue
Considerations for using generic arrays
Forced conversion of object array
aggregate
The collection of Java is like a container, which is specially used to store the references of objects. These objects can be of any data type and variable length. These collections are in Java Util's package
Collections can be divided into two categories according to their storage structure: single column Collection and double column Collection Map
Single column Collection
The root interface of a single column Set, which is used to store a series of elements that conform to a certain rule. Collection collection has two important sub interfaces: List and Set
List sets are characterized by ordered and repeatable elements
Set sets are characterized by disordered elements and no repetition
Double column set Map
Each element in the Map set contains a pair of key values, and the key is unique. You can find the corresponding value through the key
Collection interface
boolean add(Object o) -- add elements to the collection
public static void main(String[] args) { Collection collection=new ArrayList(); // Collection is an interface. ArrayList() is the implementation class of this interface. The class overrides the abstract method of collection //Through the upward transformation, collection can call the internal methods of collection, but the abstract methods are rewritten to form polymorphism collection.add("ui"); collection.add(1); collection.add("67"); System.out.println(collection); }
The type of e is object type, and any type of element can be added
boolean addAll (Collection c) -- add all elements of collection c to the collection
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add("67"); System.out.println(collection); collection.addAll(collection); System.out.println(collection); }
void clear() -- delete all elements of the collection
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add("67"); System.out.println(collection); collection.clear(); System.out.println(collection); }
boolean remove (Object o) -- deletes the specified element in the collection (only once)
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); collection.add("67"); System.out.println(collection); collection.remove(1); System.out.println(collection); }
boolean removeAll (Collection c) -- delete all elements of Collection c in this collection (delete all duplicate elements)
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); collection.add("67"); System.out.println(collection); Collection collection1=new LinkedList(); collection1.add(1); collection.removeAll(collection1); System.out.println(collection); }
boolean isEmpty() -- judge whether the collection is empty
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); System.out.println(collection); System.out.println(collection.isEmpty()); }
boolean contains (Object o) -- judge whether it contains an element
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); System.out.println(collection); System.out.println(collection.isEmpty()); System.out.println(collection.contains(1)); }
boolean containsAll (Collectio o) -- judge whether this collection contains all elements of collection o
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); System.out.println(collection); Collection collection1=new LinkedList(); collection1.add(1); collection1.add(3); System.out.println(collection1); System.out.println(collection.containsAll(collection1)); }
int size() -- get the number of elements in the collection
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); System.out.println(collection); System.out.println(collection.size()); }
object [] toArray() -- returns the array containing all elements in the set
public static void main(String[] args) { Collection collection=new ArrayList(); collection.add("ui"); collection.add(1); collection.add(1); Object[] s=collection.toArray(); for (Object o:s) { System.out.print(o+" "); } }
Generics
When the incoming element type is Object, because the Object class is the parent class of all classes, any element type can be stored
class MyarrayList { Object[] objects = new Object[10]; int usedsize = 0; public void add(Object e) { objects[usedsize++] = e; } public Object getObjects(int pos) { return objects[pos]; } public void print() { for (int i = 0; i < usedsize; i++) { System.out.print(objects[i] + " "); } System.out.println(); } } public class text { public static void main(String[] args) { MyarrayList myarrayList=new MyarrayList(); myarrayList.add("1"); myarrayList.add(1); myarrayList.add('o'); myarrayList.print(); System.out.println(myarrayList.getObjects(0)); } }
However, if we perform forced type conversion, problems often arise
String str=(String) myarrayList.getObjects(1);//Returns the Object type. Forced type conversion is required System.out.println(str);
Generics can solve this problem -- limiting the data type of the operation
Pass in parameters when instantiating a class: This instantiated object can only store String data
public static void main(String[] args) { MyarrayList<String> myarrayList=new MyarrayList<String>(); //Data of type String passed in myarrayList.add("hello"); //Data of type int passed in MyarrayList<Integer> myarrayList1=new MyarrayList<>(); myarrayList1.add(1); //Data of type char passed in MyarrayList<Character> myarrayList2=new MyarrayList<>(); myarrayList2.add('1'); }
The meaning of using generics: automatic type checking and automatic type conversion
Generic erasure mechanism
Generics have been erased as Object types during compilation
Considerations for using generic arrays
Arrays store and check type information during runtime, but generics check for errors during compilation and have been erased as Object types during runtime
You cannot use generics to define arrays
E[]objects=new E[10];--- This line of code is wrong
Reason for error:
Type E is erased as Object type during compilation. If the above code is successful, it is erased as Object type during compilation
Object[]objects=new Object[10];
However, if we forcibly convert this array into an array of String type, an error will occur. In order to avoid such an error, the underlying JVM does not allow such an array to be defined
Correct generic array definition: use mapping
Forced conversion of object array
In java, other arrays can be transformed upward as a whole into an Object array, but when the Object array is transformed downward, it cannot be transformed as a whole. Each element should be forcibly transformed separately
The object class cannot reference 8 common types. Instead, reference the wrapper class of common types
public static void main(String[] args) { String []str=new String[10]; System.out.println(str); Object[] s=new String[10];//String array can be promoted upward to Object array System.out.println(s); // String [] ret= (String[]) new Object[10];// Compile through // System.out.println(ret);// Exception thrown at runtime -- Object array cannot be forcibly converted into String array as a whole // Integer []dest= (Integer []) new Object[10];// Compile through // System.out.println(dest);// Run throw exception Object []dest=new Integer[10]; System.out.println(dest); Object a=new String[]{"hello","world"};//The Object class is a superclass of all classes System.out.println(a); }
public static void main(String[] args) { Object[] s = new String[]{"hello", "world"}; System.out.println(Arrays.toString(s)); Object[]objects=new Object[]{"hello", "world"}; String []str=new String[objects.length]; for (int i = 0; i < objects.length; i++) { str [i]=(String) objects[i]; } System.out.println(Arrays.toString(str)); }