package com.coderzheaven.arraylistcomplete; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Vector; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 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); // *********************************************************// 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<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); list.clear(); System.out.println("After Clearing"); for (String str : arrayList) System.out.println(str); // ****************************************************************// ArrayList myList = new ArrayList(5); for (int j = 0; j < 5; j++) { myList.add(new Integer(j)); } 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 k = myList.size(); k < 25; k++) { myList.add(k, new Integer(k)); } System.out.println(myList); myList.subList(10, 14).clear(); myList.trimToSize(); // ****************************************************************// IteratorDemo(); checkEmpty(); ListIteratorDemo(); } void IteratorDemo() { 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 + " "); } } void checkEmpty() { List list = Arrays.asList(new String[] { "A", "B", "C", "D" }); System.out.println(list.size()); System.out.println(list.isEmpty()); } void ListIteratorDemo() { 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 + " "); } } }
Download complete tutorial PDF from here.
Please check the LogCat for Output.
Pingback: All about ArrayList – Android » Ideal Codes