Get Sub List of
Java Array List
import
java.util.ArrayList;
import java.util.List;
public class GetSubListOfJavaArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
List lst =
arrayList.subList(1,3);
System.out.println("Sub
list contains : ");
for(int i=0; i<
lst.size() ; i++)
System.out.println(lst.get(i));
Object obj =
lst.remove(0);
System.out.println(obj
+ " is removed from sub list");
System.out.println("After
removing " + obj + " from sub list, original ArrayList contains :
");
for(int i=0; i<
arrayList.size() ; i++)
System.out.println(arrayList.get(i));
}
}
Insert all
elements of other Collection to Specified Index of Java Array List
import
java.util.ArrayList;
import
java.util.Vector;
public class
InsertAllElementsOfOtherCollectionToArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
Vector v = new
Vector();
v.add("4");
v.add("5");
arrayList.addAll(1,v);
System.out.println("After
inserting all elements of Vector at index 1,
ArrayList
contains..");
for(int i=0;
i
System.out.println(arrayList.get(i));
}
}
Iterate through
elements Java Array List using Iterator
import java.util.ArrayList;
import
java.util.Iterator;
public class
IterateThroughArrayListUsingIteratorExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Iterator itr =
arrayList.iterator();
System.out.println("Iterating
through ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());
}
}
Iterate through
elements Java Array List using List Iterator
import
java.util.ArrayList;
import
java.util.ListIterator;
public class
IterateThroughArrayListUsingListIteratorExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
ListIterator itr =
arrayList.listIterator();
System.out.println("Iterating
through ArrayList elements in forward
direction...");
while(itr.hasNext())
System.out.println(itr.next());
System.out.println("Iterating
through ArrayList elements in backward
direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Java Array List
import
java.util.ArrayList;
public class
SimpleArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
System.out.println("Getting
elements of ArrayList");
System.out.println(arrayList.get(0));
System.out.println(arrayList.get(1));
System.out.println(arrayList.get(2));
}
}
No comments:
Post a Comment