Thursday, October 30, 2014

Java Array Iterator Pro



Replace an element from Array List using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList;   
public class ReplaceAnElementUsingListIteratorExample
{   
public static void main(String[] args)
 {
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator();listIterator.next();  listIterator.set("100");
System.out.println("After replacing 1 with 100, ArrayList contains");
for(int intIndex = 0; intIndex < aList.size(); intIndex++)
System.out.println(aList.get(intIndex));
    }
}

Traverse through Array List in forward direction using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList; 
public class TraverseForwardUsingListIteratorExample
{
public static void main(String[] args)
{
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator();   
System.out.println("Traversing ArrayList in forward direction
using ListIterator");
while(listIterator.hasNext())
System.out.println(listIterator.next());
   }
}

Traverse through Array List in reverse direction using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList; 
public class TraverseReverseUsingListIteratorExample
{
public static void main(String[] args)
{
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator();
System.out.println("Traversing ArrayList in forward direction
using ListIterator");
while(listIterator.hasNext())
System.out.println(listIterator.next());
System.out.println("Traversing ArrayList in reverse direction
using ListIterator");
while(listIterator.hasPrevious())
System.out.println(listIterator.previous());
   }
}

Performing Binary Search on Java byte Array

import java.util.Arrays;
public class BinarySearchByteArrayExample
{
public static void main(String[] args)
{
byte bArray[] = {1,2,4,5};Arrays.sort(bArray); byte searchValue = 2;
int intResult = Arrays.binarySearch(bArray,searchValue);
System.out.println("Result of binary search of 2 is : " + intResult);
searchValue = 3;
intResult = Arrays.binarySearch(bArray,searchValue);
System.out.println("Result of binary search of 3 is : " + intResult);
   }
}

Performing Binary Search on Java char Array

import java.util.Arrays; 
public class BinarySearchCharArrayExample
{
public static void main(String[] args)
{
char charArray[] = {'a','b','d','e'};  Arrays.sort(charArray);  char searchValue = 'b';
int intResult = Arrays.binarySearch(charArray,searchValue);
System.out.println("Result of binary search of 'b' is : " + intResult);
searchValue = 'c';
intResult = Arrays.binarySearch(charArray,searchValue);
System.out.println("Result of binary search of 'c' is : " + intResult);
   }
}

No comments:

Post a Comment