Collection Collection
Collection derives two sub-interfaces, one is list and the other is set.
list: called a repeatable set, as the name implies, the collection is allowed to hold repeating elements, then what is the repeating element? A repeating element refers to an element that is not the same element, but rather the Equals method that compares to true.
set: called a non-repeating set, so that the same element cannot be stored in the collection two times, the same as the list, where the same refers to the two element equals the result of a comparison is true.
List: interface has two implementation classes:ArrayList is more suitable for random access and LinkedList is more suitable for inserting and deleting
- E get (int index): Gets the element that corresponds to the specified subscript in the collection, starting with the subscript 0.
- e Set (int index, E elment): Places the given element in the given position and returns the element from the original position
-
- void Add(int index,E element):
Inserts the given element into the specified position, and the original position and subsequent elements are moved backwards in order.
- E Remove(int index):
Deletes the element at the given position and returns the element that was deleted.
sublist Gets a list that occupies the same storage space as the original list, and the operation of the sub list affects the original list.
- list<integer> List = new ArrayList<integer> ();
- For (int i = 0; I < ten; I+ +) {
- List. Add(i);
- }
- System. Out. println(list); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- list<integer> sublist = List. Sublist(3, 8);
- System. Out. println(sublist); //[3, 4, 5, 6, 7]
- The list and source list obtained by sublist occupy the same data space
- For (int i = 0; I < sublist. Size(); I+ +) {
- Sublist. Set(i, sublist. Get(i) * ten);
- }
- System. Out. println(sublist); //[ +, +, +]
- System. Out. println(list); //[0, 1, 2, +, 8, 9]
- Can be used to delete continuous elements list.sublist (3, 8). Clear ();
- System. Out. println(list);
List conversions to arrays
- list<string> List = new ArrayList<string> ();
- List. Add("a");
- List. Add("B");
- List. Add("C");
- Usually we pass in an array that does not require a given length
- String[] strarr = List. ToArray(new String[] {}); System. Out. println(Arrays. ToString(Strarr)); //[A, B, c]
Array conversion to List
- String[] strarr = { "a", "B", "C" };
- list<string> List = Arrays. Aslist(Strarr);
- System. Out. println(list); //[A, B, c]
- List.add ("D"); Will throw Unsupportedoperationexception
- Java.util.arrays$arraylist
- System. Out. println(list. GetClass(). GetName());
- list<string> List1 = new ArrayList<string> ();
- List1. AddAll(Arrays. Aslist(Strarr));
Collection, Map