Array List example programs
- How to Iterate ArrayList using Java ListIterator Example
- Arraylist add element at specific index
- Sort ArrayList in descending order
- Java program to insert an element to ArrayList using ListIterator Example
- How to sort arraylist of strings alphabetically java
- Java Program to Sort elements of Java ArrayList Example
- Basic Java example program to replace an element at specified index arraylist
- Search an element of Java ArrayList Example
- Java Example Program to Remove element from specified index ArrayList
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Comparator;
import java.util.Scanner;
public class CollectionLists {
public static void main(String [] args){
ArrayList<String> arrayList=new ArrayList<String>();
//adding elements to list
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
arrayList.add("G");
ListIterator<String> itr=arrayList.listIterator();
/*Use hasNext() and next()
* method to iterate through forward direction
*/
System.out.println("Array List in forward direction");
while(itr.hasNext()){
System.out.println(itr.next());
}
/*Use hasPrevious() and previous()
* method to iterate through backward direction
*/
System.out.println("Array List in backward direction");
while(itr.hasPrevious()){
System.out.println(itr.previous());
}
System.out.println("\n Adding element at specified index");
//adding element at specific index
arrayList.add(1,"a");
System.out.println(arrayList+"\n");
System.out.println("Sorting ArrayList in descending order");
//Sorting array in descending order
ArrayList<Integer> arrayList1=new ArrayList<Integer>();
//adding elements
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
arrayList1.add(4);
arrayList1.add(5);
arrayList1.add(6);
arrayList1.add(7);
/*Using static comparator reverseOrder() method of
* collections utility class to get comparator object
*/
Comparator<Integer> comparator=Collections.reverseOrder();
System.out.println("Before sorting "+arrayList1);
/*Using static void sort(List list,Comparator comp)
* method of collection class
*/
Collections.sort(arrayList1,comparator);
System.out.println("After sorting "+arrayList1+"\n");
/*
* use Collections.sort(arrayList1) for Ascending order
*/
System.out.println("Inserting element in Array List using ListIterator");
ListIterator<Integer> litr=arrayList1.listIterator();
System.out.println("Before inserting "+arrayList1);
litr.next(); //if this section is commented 9 will be printed first followed by 7
litr.add(9);
System.out.println("After sorting "+arrayList1+"\n");
System.out.println("Replace an element at specified index in Array List");
System.out.println("Before Replacing "+arrayList1);
arrayList1.set(1, 10);
System.out.println("After Replacing "+arrayList1+"\n" );
System.out.println("Search an element in ArrayList and get the index");
Scanner sc=new Scanner(System.in);
System.out.println("Type the number you want to search");
int x=sc.nextInt();
boolean isContains=arrayList1.contains(x);
int index=arrayList1.indexOf(x);
System.out.println("Does ArrayList contain "+x+"? "+isContains);
if(index==-1)
System.out.println("ArrayList does not contain "+x);
else
System.out.println("ArrayList does contain "+x+ " and it's index is "+index);
System.out.println("See for yourself "+arrayList1+"\n");
System.out.println("Remove an element from ArrayList based on index");
Object obj=arrayList1.remove(1);
System.out.println(obj+" has been removed from the list");
System.out.println("See for yourself "+arrayList1);
}
}
Output:
Array List in forward direction
A
B
C
D
E
F
G
Array List in backward direction
G
F
E
D
C
B
A
Adding element at specified index
[A, a, B, C, D, E, F, G]
Sorting ArrayList in descending order
Before sorting [1, 2, 3, 4, 5, 6, 7]
After sorting [7, 6, 5, 4, 3, 2, 1]
Inserting element in Array List using ListIterator
Before inserting [7, 6, 5, 4, 3, 2, 1]
After sorting [7, 9, 6, 5, 4, 3, 2, 1]
Replace an element at specified index in Array List
Before Replacing [7, 9, 6, 5, 4, 3, 2, 1]
After Replacing [7, 10, 6, 5, 4, 3, 2, 1]
Search an element in ArrayList and get the index
Type the number you want to search
3
Does ArrayList contain 3? true
ArrayList does contain 3 and it's index is 5
See for yourself [7, 10, 6, 5, 4, 3, 2, 1]
Remove an element from ArrayList based on index
10 has been removed from the list
See for yourself [7, 6, 5, 4, 3, 2, 1]
Comments
Post a Comment