Java by API/java.util/ArrayList
Содержание
- 1 AbstractList: subList(int fromIndex, int toIndex)
- 2 ArrayList: addAll(Collection<?> c)
- 3 ArrayList: addAll(int index, Collection<?> c)
- 4 ArrayList: add(E o)
- 5 ArrayList: add(int index, E element)
- 6 ArrayList: clear()
- 7 ArrayList: contains(Object elem)
- 8 ArrayList: ensureCapacity(int minCapacity)
- 9 ArrayList: equals(Object o)
- 10 ArrayList: get(int index)
- 11 ArrayList: indexOf(Object elem)
- 12 ArrayList: isEmpty()
- 13 ArrayList: iterator()
- 14 ArrayList: lastIndexOf(Object o)
- 15 ArrayList: listIterator()
- 16 ArrayList: removeAll(Collection<?> c)
- 17 ArrayList: remove(int index)
- 18 ArrayList: remove(Object o)
- 19 ArrayList: retainAll(Collection c)
- 20 ArrayList: set(int index, T element)
- 21 ArrayList: size()
- 22 ArrayList: toArray()
- 23 ArrayList: toArray(T[] a)
- 24 ArrayList: trimToSize()
- 25 for each loop for ArrayList
- 26 new ArrayList(Collection<?> c)
- 27 new ArrayList < E > ()
AbstractList: subList(int fromIndex, int toIndex)
/*
List contains 5 elements
List contains Integer(2): true
Integer(2) is at index 2
Get element at index 2: 99
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 15
at java.util.SubList.<init>(AbstractList.java:705)
at java.util.RandomAccessSubList.<init>(AbstractList.java:861)
at java.util.AbstractList.subList(AbstractList.java:570)
at MainClass.main(MainClass.java:23)
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args) {
ArrayList myList = new ArrayList(5);
for (int i = 0; i < 5; i++) {
myList.add(new Integer(i));
}
System.out.println("List contains " + myList.size() + " elements");
Integer int2 = new Integer(2);
System.out.println("List contains Integer(2): " + myList.contains(int2));
System.out.println("Integer(2) is at index " + myList.indexOf(int2));
myList.set(2, new Integer(99));
System.out.println("Get element at index 2: " + myList.get(2));
myList.ensureCapacity(15);
for (int i = 10; i < 15; i++) {
myList.add(new Integer(i));
}
myList.subList(10, 15).clear();
myList.trimToSize();
System.out.println(myList);
}
}
ArrayList: addAll(Collection<?> c)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
List<String> list2 = new ArrayList<String>();
list2.add("X");
list2.add("Y");
list2.add("Z");
list.addAll(list2);
list.addAll(1, list2);
System.out.println(list);
}
}
ArrayList: addAll(int index, Collection<?> c)
import java.util.ArrayList;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
Vector<String> v = new Vector<String>();
v.add("4");
v.add("5");
// insert all elements of Vector to ArrayList at index 1
arrayList.addAll(1, v);
for (String str: arrayList)
System.out.println(str);
}
}
ArrayList: add(E o)
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
ArrayList: add(int index, E element)
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
ArrayList: clear()
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.clear();
System.out.println(list);
}
}
ArrayList: contains(Object elem)
/*
List contains 5 elements
List contains Integer(2): true
Integer(2) is at index 2
Get element at index 2: 99
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 15
at java.util.SubList.<init>(AbstractList.java:705)
at java.util.RandomAccessSubList.<init>(AbstractList.java:861)
at java.util.AbstractList.subList(AbstractList.java:570)
at MainClass.main(MainClass.java:23)
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args) {
ArrayList myList = new ArrayList(5);
for (int i = 0; i < 5; i++) {
myList.add(new Integer(i));
}
System.out.println("List contains " + myList.size() + " elements");
Integer int2 = new Integer(2);
System.out.println("List contains Integer(2): " + myList.contains(int2));
System.out.println("Integer(2) is at index " + myList.indexOf(int2));
myList.set(2, new Integer(99));
System.out.println("Get element at index 2: " + myList.get(2));
myList.ensureCapacity(15);
for (int i = 10; i < 15; i++) {
myList.add(new Integer(i));
}
myList.subList(10, 15).clear();
myList.trimToSize();
System.out.println(myList);
}
}
ArrayList: ensureCapacity(int minCapacity)
/*
List contains 5 elements
List contains Integer(2): true
Integer(2) is at index 2
Get element at index 2: 99
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 15
at java.util.SubList.<init>(AbstractList.java:705)
at java.util.RandomAccessSubList.<init>(AbstractList.java:861)
at java.util.AbstractList.subList(AbstractList.java:570)
at MainClass.main(MainClass.java:23)
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args) {
ArrayList myList = new ArrayList(5);
for (int i = 0; i < 5; i++) {
myList.add(new Integer(i));
}
System.out.println("List contains " + myList.size() + " elements");
Integer int2 = new Integer(2);
System.out.println("List contains Integer(2): " + myList.contains(int2));
System.out.println("Integer(2) is at index " + myList.indexOf(int2));
myList.set(2, new Integer(99));
System.out.println("Get element at index 2: " + myList.get(2));
myList.ensureCapacity(15);
for (int i = 10; i < 15; i++) {
myList.add(new Integer(i));
}
myList.subList(10, 15).clear();
myList.trimToSize();
System.out.println(myList);
}
}
ArrayList: equals(Object o)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
List list2 = Arrays.asList(new String[] { "A", "B", "C" });
System.out.println(list.equals(list2));
}
}
ArrayList: get(int index)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.get(2));
}
}
ArrayList: indexOf(Object elem)
/*
List contains 5 elements
List contains Integer(2): true
Integer(2) is at index 2
Get element at index 2: 99
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 15
at java.util.SubList.<init>(AbstractList.java:705)
at java.util.RandomAccessSubList.<init>(AbstractList.java:861)
at java.util.AbstractList.subList(AbstractList.java:570)
at MainClass.main(MainClass.java:23)
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args) {
ArrayList myList = new ArrayList(5);
for (int i = 0; i < 5; i++) {
myList.add(new Integer(i));
}
System.out.println("List contains " + myList.size() + " elements");
Integer int2 = new Integer(2);
System.out.println("List contains Integer(2): " + myList.contains(int2));
System.out.println("Integer(2) is at index " + myList.indexOf(int2));
myList.set(2, new Integer(99));
System.out.println("Get element at index 2: " + myList.get(2));
myList.ensureCapacity(15);
for (int i = 10; i < 15; i++) {
myList.add(new Integer(i));
}
myList.subList(10, 15).clear();
myList.trimToSize();
System.out.println(myList);
}
}
ArrayList: isEmpty()
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
System.out.println(list.size());
System.out.println(list.isEmpty());
}
}
ArrayList: iterator()
/**
*Output:
Original contents of al: C A E B D F
Modified list backwards: F+ D+ B+ E+ A+ C+
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}
}
ArrayList: lastIndexOf(Object o)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
System.out.println(list.lastIndexOf("A"));
}
}
ArrayList: listIterator()
/**
*Output:
Original contents of al: C A E B D F
Modified list backwards: F+ D+ B+ E+ A+ C+
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}
}
ArrayList: removeAll(Collection<?> c)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("A");
list.add("B");
list.add("B");
list.add("C");
list.add("C");
List<String> list2 = new ArrayList<String>();
list2.add("A");
list2.add("B");
list2.add("C");
list.removeAll(list2);
System.out.println(list);
}
}
ArrayList: remove(int index)
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
ArrayList: remove(Object o)
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
ArrayList: retainAll(Collection c)
/*
Output:
true
[2nd, 3rd, 2nd, 3rd]
* */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainClass {
public static void main(String args[]) {
String orig[] = { "1st", "2nd", "3rd", "4th", "5th", "1st", "2nd", "3rd",
"4th", "5th" };
String act[] = { "2nd", "3rd", "6th" };
List origList = new ArrayList(Arrays.asList(orig));
List actList = Arrays.asList(act);
System.out.println(origList.retainAll(actList));
System.out.println(origList);
}
}
ArrayList: set(int index, T element)
import java.util.ArrayList;
public class Main {
public static void main(String[] a) {
ArrayList<String> nums = new ArrayList<String>();
nums.clear();
nums.add("One");
nums.add("Two");
nums.add("Three");
System.out.println(nums);
nums.set(0, "Uno");
nums.set(1, "Dos");
nums.set(2, "Tres");
System.out.println(nums);
}
}
ArrayList: size()
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
ArrayList: toArray()
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Object[] objArray = arrayList.toArray();
for (Object obj : objArray)
System.out.println(obj);
}
}
ArrayList: toArray(T[] a)
/**
*Output:
Contents of al: [1, 2, 3, 4]
Sum is: 10 */
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
System.out.println("Contents of al: " + al);
Integer ia[] = new Integer[al.size()];
ia = al.toArray(ia);
int sum = 0;
for (int i : ia)
sum += i;
System.out.println("Sum is: " + sum);
}
}
ArrayList: trimToSize()
/*
List contains 5 elements
List contains Integer(2): true
Integer(2) is at index 2
Get element at index 2: 99
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 15
at java.util.SubList.<init>(AbstractList.java:705)
at java.util.RandomAccessSubList.<init>(AbstractList.java:861)
at java.util.AbstractList.subList(AbstractList.java:570)
at MainClass.main(MainClass.java:23)
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args) {
ArrayList myList = new ArrayList(5);
for (int i = 0; i < 5; i++) {
myList.add(new Integer(i));
}
System.out.println("List contains " + myList.size() + " elements");
Integer int2 = new Integer(2);
System.out.println("List contains Integer(2): " + myList.contains(int2));
System.out.println("Integer(2) is at index " + myList.indexOf(int2));
myList.set(2, new Integer(99));
System.out.println("Get element at index 2: " + myList.get(2));
myList.ensureCapacity(15);
for (int i = 10; i < 15; i++) {
myList.add(new Integer(i));
}
myList.subList(10, 15).clear();
myList.trimToSize();
System.out.println(myList);
}
}
for each loop for ArrayList
/**
*Output:
Original contents of vals: 1 2 3 4 5
Sum of values: 15
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<Integer> vals = new ArrayList<Integer>();
vals.add(1);
vals.add(2);
vals.add(3);
vals.add(4);
vals.add(5);
System.out.print("Original contents of vals: ");
for (int v : vals)
System.out.print(v + " ");
System.out.println();
int sum = 0;
for (int v : vals)
sum += v;
System.out.println("Sum of values: " + sum);
}
}
new ArrayList(Collection<?> c)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.add("Hello");
queue.add("World");
List<String> list = new ArrayList<String>(queue);
System.out.println(list);
}
}
new ArrayList < E > ()
/**
*Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
*/
import java.util.ArrayList;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}