Dec 15

How to remove element from list in java?

[pullquote]To remove an element from list ArrayList remove method is used.we can remove object from collection or remove element based on index.[/pullquote]

 

 

 

 

 

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class ArrayListRemoveDemo {
public static void main(String[] args) {
//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding duplicate elements to the list
list.add("One");
list.add("Two");
list.add("Three");

//Adding element D at zeroth postion
list.add(0,"Four");

System.out.println(" Printing elements before removing elements "+list);

//Removing element from list using remove method
list.remove("One");

System.out.println(" Printing elements after removal of element "+list);

//Removing element from list using remove method based on index
list.remove(2);
System.out.println(" Printing elements after removal of element based on index "+list);

}

}

[/code]

Output:

Printing elements before removing elements [Four, One, Two, Three]
Printing elements after removal of element [Four, Two, Three]
Printing elements after removal of element based on index [Four, Two]

Dec 15

How to remove all elements from arraylist ?

[pullquote]To remove all elements from list clear method is used .[/pullquote]

 

 

 

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class ArrayListClearDemo {

public static void main(String[] args) {
//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding duplicate elements to the list
list.add("One");
list.add("Two");
list.add("Three");

//Adding element D at zeroth postion
list.add(0,"Four");

System.out.println(list);

//clear method is used to remove all elements from list.

list.clear();

System.out.println(list);
}

}

[/code]

Output:

[Four, One, Two, Three]
[]

Dec 15

How to add a collection to arraylist in java?

[pullquote]ArrayList addAll method is used to add a collection to end of  arraylist.[/pullquote]

 

 

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class ArrayListAddAllDemo {
public static void main(String[] args) {

// creating list with two elements one and two
List<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
System.out.println(list);

List<String> list1=new ArrayList<String>();
list1.add("zero");

//addAll method is used to append elements to the end of the list.

list1.addAll(list);

System.out.println(list1);
}

}
[/code]

Output:

[one, two]
[zero, one, two]

Dec 15

How to retrieve elements in arraylist using Iterator?

[pullquote]To Iterate through the elements in collection we have iterator method in util package[/pullquote]

 

[code lang=”java”]import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListDemo {
public static void main(String[] args) {

//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding duplicate elements to the list
list.add("One");
list.add("Two");
list.add("Three");

//Adding element D at zeroth postion
list.add(0,"Four");

//Iterating elements in arraylist using Iterator

Iterator<String> it=list.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}

}

}

[/code]

Output:

Four
One
Two
Three

Dec 15

How to add elements to arraylist at specified position?

[pullquote]In ArrayList we have add method which takes two arguments index and element.By using this method we can add elements to arraylist based on position.[/pullquote]

 

 

 

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class AddArrayListDemo {
public static void main(String[] args) {

//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding duplicate elements to the list
list.add("A");
list.add("B");
list.add("C");

//Adding element D at zeroth postion
list.add(0,"D");

//Printing elements in ArrayList
System.out.println(list);

}

}

[/code]

Output:

[D, A, B, C]

Dec 14

Example to show arraylist in java allows duplicates

[pullquote]
Duplicates can be stored in arraylist.while set and map doesn’t allow duplicates.Map doesn’t allow duplicate keys but allows duplicate values.
[/pullquote]

 

 

 

 

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class AddArrayListDemo {
public static void main(String[] args) {

//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding duplicate elements to the list
list.add("A");
list.add("A");
list.add("A");

//Printing elements in ArrayList
System.out.println(list);

}

}
[/code]

Dec 14

How to add elements to arraylist in java?

[code lang=”java”]import java.util.ArrayList;
import java.util.List;

public class AddArrayListDemo {
public static void main(String[] args) {

//creating arraylist which can hold strings
List<String> list=new ArrayList<String>();

//adding elements to the list
list.add("Red");
list.add("green");
list.add("yellow");

}

}[/code]

[pullquote]To add elements to arraylist we have add method in List interface which returns
boolean value true if element is added to list else it returns false[/pullquote]

Dec 02

How to copy array values in java?

[code lang=”java”]public class ArraysCopyDemo {

public static void main(String[] args) {
//Initializing array with values
String[] string={"one","two","three"};
//Creating new array
String []copiedArray=new String[3];

//Copying values to copiedArray from String array.
System.arraycopy(string, 0, copiedArray, 0, 3);

System.out.println("Printing Original Array");
for (String string2 : string) {
System.out.println(string2);
}
System.out.println("Printing Copied Array");
for (String string2 : copiedArray) {
System.out.println(string2);
}
}

}[/code]

Output:

Printing Original Array
one
two
three
Printing Copied Array
one
two
three

[pullquote]ArrayCopy method is used to copy contents from source array to target array by starting position till destination position[/pullquote]