You are on page 1of 12

Add an element to specified index of Java ArrayList Example

1. /*
2.   Add an element to specified index of Java ArrayList Example
3.   This Java Example shows how to add an element at specified index of
java
4.   ArrayList object using add method.
5. */
6.  
7. import java.util.ArrayList;
8.  
9. public class AddElementToSpecifiedIndexArrayListExample {
10.  
11. public static void main(String[] args) {
12. //create an ArrayList object
13. ArrayList arrayList = new ArrayList();
14.  
15. //Add elements to Arraylist
16. arrayList.add("1");
17. arrayList.add("2");
18. arrayList.add("3");
19.  
20. /*
21.   To add an element at the specified index of ArrayList use
22.   void add(int index, Object obj) method.
23.   This method inserts the specified element at the specified index
in the
24.   ArrayList.
25.   */
26. arrayList.add(1,"INSERTED ELEMENT");
27.  
28. /*
29.   Please note that add method DOES NOT overwrites the element
previously
30.   at the specified index in the list. It shifts the elements to
right side
31.   and increasing the list size by 1.
32.   */
33.  
34. System.out.println("ArrayList contains...");
35. //display elements of ArrayList
36. for(int index=0; index < arrayList.size(); index++)
37. System.out.println(arrayList.get(index));
38.  
39. }
40. }
41.  
42. /*
43. Output would be
44. ArrayList contains...
45. 1
46. INSERTED ELEMENT
47. 2
48. 3
49. */

1
Append all elements of other Collection to Java ArrayList Example
1. /*
2.   Append all elements of other Collection to Java ArrayList Example
3.   This Java Example shows how to append all elements of other Collection
object
4.   at the end of Java ArrayList object using addAll method. This program
shows
5.   how to append all elements of Java Vector to Java ArrayList object.
6. */
7.  
8. import java.util.ArrayList;
9. import java.util.Vector;
10.  
11. public class AppendAllElementsOfOtherCollectionToArrayListExample {
12.  
13. public static void main(String[] args) {
14.  
15. //create an ArrayList object
16. ArrayList arrayList = new ArrayList();
17.  
18. //Add elements to Arraylist
19. arrayList.add("1");
20. arrayList.add("2");
21. arrayList.add("3");
22.  
23. //create a new Vector object
24. Vector v = new Vector();
25. v.add("4");
26. v.add("5");
27.  
28. /*
29.   To append all elements of another Collection to ArrayList use
30.   boolean addAll(Collection c) method.
31.   It returns true if the ArrayList was changed by the method call.
32.   */
33.  
34. //append all elements of Vector to ArrayList
35. arrayList.addAll(v);
36.  
37. //display elements of ArrayList
38. System.out.println("After appending all elements of Vector,
39. ArrayList contains..");
40. for(int i=0; i<arrayList.size(); i++)
41. System.out.println(arrayList.get(i));
42.  
43. }
44. }
45.  
46. /*
47. Output would be
48. After appending all elements of Vector, ArrayList contains..
49. 1
50. 2
51. 3
52. 4

2
53. 5
54. */

Copy all elements of Java ArrayList to an Object Array Example


1. /*
2.   Copy all elements of Java ArrayList to an Object Array Example
3.   This Java Example shows how to copy all elements of Java ArrayList
object to an
4.   array of Objects using toArray method.
5. */
6.  
7. import java.util.ArrayList;
8.  
9. public class CopyElementsOfArrayListToArrayExample {
10.  
11. public static void main(String[] args) {
12. //create an ArrayList object
13. ArrayList arrayList = new ArrayList();
14.  
15. //Add elements to ArrayList
16. arrayList.add("1");
17. arrayList.add("2");
18. arrayList.add("3");
19. arrayList.add("4");
20. arrayList.add("5");
21.  
22. /*
23.   To copy all elements of java ArrayList object into array use
24.   Object[] toArray() method.
25.   */
26.  
27. Object[] objArray = arrayList.toArray();
28.  
29. //display contents of Object array
30. System.out.println("ArrayList elements are copied into an Array.
31. Now Array
Contains..");
32. for(int index=0; index < objArray.length ; index++)
33. System.out.println(objArray[index]);
34. }
35. }
36.  
37. /*
38. Output would be
39. ArrayList elements are copied into an Array. Now Array Contains..
40. 1
41. 2
42. 3
43. 4
44. 5
45. */

3
Get Size of Java ArrayList and loop through elements Example
1. /*
2.   Get Size of Java ArrayList and loop through elements Example
3.   This Java Example shows how to get size or number of elements
currently
4.   stored in ArrayList. It also shows how to loop through element of it.
5. */
6.  
7. import java.util.ArrayList;
8.  
9. public class GetSizeOfArrayListExample {
10.  
11. public static void main(String[] args) {
12. //create an ArrayList object
13. ArrayList arrayList = new ArrayList();
14.  
15. //Add elements to Arraylist using
16. arrayList.add("1");
17. arrayList.add("2");
18. arrayList.add("3");
19.  
20. //To get size of Java ArrayList use int size() method
21. int totalElements = arrayList.size();
22.  
23. System.out.println("ArrayList contains...");
24. //loop through it
25. for(int index=0; index < totalElements; index++)
26. System.out.println(arrayList.get(index));
27.  
28. }
29. }
30.  
31. /*
32. Output would be
33. ArrayList contains...
34. 1
35. 2
36. 3
37. */

Get Sub List of Java ArrayList Example


1. /*
2.   Get Sub List of Java ArrayList Example
3.   This Java Example shows how to get sub list of java ArrayList using
subList
4.   method by providing start and end index.
5. */
6.  
7. import java.util.ArrayList;
8. import java.util.List;
9.  
10. public class GetSubListOfJavaArrayListExample {
11.  

4
12. public static void main(String[] args) {
13.  
14. //create an ArrayList object
15. ArrayList arrayList = new ArrayList();
16.  
17. //Add elements to Arraylist
18. arrayList.add("1");
19. arrayList.add("2");
20. arrayList.add("3");
21. arrayList.add("4");
22. arrayList.add("5");
23.  
24. /*
25.   To get a sub list of Java ArrayList use
26.   List subList(int startIndex, int endIndex) method.
27.   This method returns an object of type List containing elements
from
28.   startIndex to endIndex - 1.
29.   */
30.  
31. List lst = arrayList.subList(1,3);
32.  
33. //display elements of sub list.
34. System.out.println("Sub list contains : ");
35. for(int i=0; i< lst.size() ; i++)
36. System.out.println(lst.get(i));
37.  
38. /*
39.   Sub List returned by subList method is backed by original
Arraylist. So any
40.   changes made to sub list will also be REFLECTED in the original
Arraylist.
41.   */
42. //remove one element from sub list
43. Object obj = lst.remove(0);
44. System.out.println(obj + " is removed from sub list");
45.  
46. //print original ArrayList
47. System.out.println("After removing " + obj + " from sub list,
original ArrayList contains : ");
48. for(int i=0; i< arrayList.size() ; i++)
49. System.out.println(arrayList.get(i));
50.  
51. }
52.  
53. }
54. /*
55. Output would be
56. Sub list contains :
57. 2
58. 3
59. 2 is removed from sub list
60. After removing 2 from sub list original ArrayList contains :
61. 1
62. 3
63. 4
64. 5

5
65. */

Insert all elements of other Collection to Specified Index of Java


ArrayList Example
1. /*
2.   Insert all elements of other Collection to Specified Index of Java
3.   ArrayList Example
4.   This Java Example shows how to insert all elements of other Collection
object
5.   at specified index of Java ArrayList object using addAll method.
6. */
7.  
8. import java.util.ArrayList;
9. import java.util.Vector;
10.  
11. public class InsertAllElementsOfOtherCollectionToArrayListExample {
12.  
13. public static void main(String[] args) {
14.  
15. //create an ArrayList object
16. ArrayList arrayList = new ArrayList();
17.  
18. //Add elements to Arraylist
19. arrayList.add("1");
20. arrayList.add("2");
21. arrayList.add("3");
22.  
23. //create a new Vector object
24. Vector v = new Vector();
25. v.add("4");
26. v.add("5");
27.  
28. /*
29.   To insert all elements of another Collection to sepcified index
of ArrayList
30.   use
31.   boolean addAll(int index, Collection c) method.
32.   It returns true if the ArrayList was changed by the method call.
33.   */
34.  
35. //insert all elements of Vector to ArrayList at index 1
36. arrayList.addAll(1,v);
37.  
38. //display elements of ArrayList
39. System.out.println("After inserting all elements of Vector at
index 1,
40. ArrayList
contains..");
41. for(int i=0; i<arrayList.size(); i++)
42. System.out.println(arrayList.get(i));
43.  
44. }
45. }
46.  

6
47. /*
48. Output would be
49. After inserting all elements of Vector at index 1, ArrayList
contains..
50. 1
51. 4
52. 5
53. 2
54. 3
55. */

Iterate through elements Java ArrayList using Iterator Example

1. /*
2.   Iterate through elements Java ArrayList using Iterator Example
3.   This Java Example shows how to iterate through the elements of java
4.   ArrayList object using Iterator.
5. */
6.  
7. import java.util.ArrayList;
8. import java.util.Iterator;
9.  
10. public class IterateThroughArrayListUsingIteratorExample {
11.  
12. public static void main(String[] args) {
13.  
14. //create an ArrayList object
15. ArrayList arrayList = new ArrayList();
16.  
17. //Add elements to Arraylist
18. arrayList.add("1");
19. arrayList.add("2");
20. arrayList.add("3");
21. arrayList.add("4");
22. arrayList.add("5");
23.  
24. //get an Iterator object for ArrayList using iterator() method.
25. Iterator itr = arrayList.iterator();
26.  
27. //use hasNext() and next() methods of Iterator to iterate through
the elements
28. System.out.println("Iterating through ArrayList elements...");
29. while(itr.hasNext())
30. System.out.println(itr.next());
31.  
32. }
33. }
34.  
35. /*
36. Output would be
37. Iterating through ArrayList elements...
38. 1
39. 2

7
40. 3
41. 4
42. 5
43. */

Iterate through elements Java ArrayList using ListIterator Example


1. /*
2.   Iterate through elements Java ArrayList using ListIterator Example
3.   This Java Example shows how to iterate through the elements of java
4.   ArrayList object in forward and backward direction using ListIterator.
5. */
6.  
7. import java.util.ArrayList;
8. import java.util.ListIterator;
9.  
10. public class IterateThroughArrayListUsingListIteratorExample {
11.  
12. public static void main(String[] args) {
13.  
14. //create an ArrayList object
15. ArrayList arrayList = new ArrayList();
16.  
17. //Add elements to Arraylist
18. arrayList.add("1");
19. arrayList.add("2");
20. arrayList.add("3");
21. arrayList.add("4");
22. arrayList.add("5");
23.  
24. /*
25.   Get a ListIterator object for ArrayList using
26.   listIterator() method.
27.   */
28. ListIterator itr = arrayList.listIterator();
29.  
30. /*
31.   Use hasNext() and next() methods of ListIterator to iterate
through
32.   the elements in forward direction.
33.   */
34. System.out.println("Iterating through ArrayList elements in
forward
35.
direction...");
36. while(itr.hasNext())
37. System.out.println(itr.next());
38.  
39. /*
40.   Use hasPrevious() and previous() methods of ListIterator to
iterate through
41.   the elements in backward direction.
42.   */
43. System.out.println("Iterating through ArrayList elements in
backward

8
44.
direction...");
45. while(itr.hasPrevious())
46. System.out.println(itr.previous());
47.  
48. }
49. }
50.  
51. /*
52. Output would be
53. Iterating through ArrayList elements...
54. Iterating through ArrayList elements in forward direction...
55. 1
56. 2
57. 3
58. 4
59. 5
60. Iterating through ArrayList elements in backward direction...
61. 5
62. 4
63. 3
64. 2
65. 1
66. */

Compare Two Java boolean Arrays Example

1. /*
2.   Compare Two Java boolean Arrays Example
3.   This java example shows how to compare two boolean arrays for equality
using
4.   Arrays.equals method.
5. */
6.  
7. import java.util.Arrays;
8.  
9. public class CompareBooleanArraysExample {
10.  
11. public static void main(String[] args) {
12. //create boolean arrays
13. boolean[] blnArray1 = new boolean[]{true,false,true};
14. boolean[] blnArray2 = new boolean[]{true,false,true};
15.  
16. /*
17.   To compare two boolean arrays use,
18.   static boolean equals(boolean array1[], boolean array2[])
19.   method of Arrays class.
20.  
21.   It returns true if both arrays are equal. Arrays are considered
as equal
22.   if they contain same elements in same order.
23.   */
24.  

9
25. boolean blnResult = Arrays.equals(blnArray1,blnArray2);
26. System.out.println("Are two boolean arrays equal ? : " +
blnResult);
27.  
28. /*
29.   Please note that two boolean array references pointing to null
are
30.   considered as equal.
31.   */
32.  
33. }
34. }
35.  
36. /*
37. Output of the program would be
38. Are two boolean arrays equal ? : true
39. */
40. /*
41.   Compare Two Java byte Arrays Example
42.   This java example shows how to compare two byte arrays for equality
using
43.   Arrays.equals method.
44. */

Compare Two Java byte Arrays Example

45. import java.util.Arrays;


46.  
47. public class CompareByteArraysExample {
48.  
49. public static void main(String[] args) {
50. //create byte arrays
51. byte[] byteArray1 = new byte[]{7,25,12};
52. byte[] byteArray2 = new byte[]{7,25,12};
53.  
54. /*
55.   To compare two byte arrays use,
56.   static boolean equals(byte array1[], byte array2[]) method of
Arrays class.
57.  
58.   It returns true if both arrays are equal. Arrays are considered
as equal
59.   if they contain same elements in same order.
60.   */
61.  
62. boolean blnResult = Arrays.equals(byteArray1,byteArray2);
63. System.out.println("Are two byte arrays equal ? : " + blnResult);
64.  
65. /*
66.   Please note that two byte array references pointing to null are
67.   considered as equal.
68.   */
69.  

10
70. }
71. }
72.  
73. /*
74. Output of the program would be
75. Are two byte arrays equal ? : true
76. */

1. /*
2.   Copy Elements of ArrayList to Java Vector Example
3.   This java example shows how to copy elements of Java ArrayList to Java
Vector using
4.   copy method of Collections class.
5. */
6.  
7. import java.util.ArrayList;
8. import java.util.Collections;
9. import java.util.Vector;
10.  
11. public class CopyElementsOfArrayListToVectorExample {
12.  
13. public static void main(String[] args) {
14. //create an ArrayList object
15. ArrayList arrayList = new ArrayList();
16.  
17. //Add elements to Arraylist
18. arrayList.add("1");
19. arrayList.add("4");
20. arrayList.add("2");
21. arrayList.add("5");
22. arrayList.add("3");
23.  
24. //create a Vector object
25. Vector v = new Vector();
26.  
27. //Add elements to Vector
28. v.add("A");
29. v.add("B");
30. v.add("D");
31. v.add("E");
32. v.add("F");
33. v.add("G");
34. v.add("H");
35.  
36. /*
37.   To copy elements of Java ArrayList to Java Vector use,
38.   static void copy(List dstList, List sourceList) method of
Collections class.
39.  
40.   This method copies all elements of source list to destination
list. After copy
41.   index of the elements in both source and destination lists would
be identical.
42.  

11
43.   The destination list must be long enough to hold all copied
elements. If it is
44.   longer than that, the rest of the destination list's elments
would remain
45.   unaffected.
46.   */
47.  
48. System.out.println("Before copy, Vector Contains : " + v);
49.  
50. //copy all elements of ArrayList to Vector using copy method of
Collections class
51. Collections.copy(v,arrayList);
52.  
53. /*
54.   Please note that, If Vector is not long enough to hold all
elements of
55.   ArrayList, it throws IndexOutOfBoundsException.
56.   */
57.  
58. System.out.println("After Copy, Vector Contains : " + v);
59. }
60. }
61.  
62. /*
63. Output would be
64. Before copy Vector Contains : [A, B, D, E, F, G, H]
65. After Copy Vector Contains : [1, 4, 2, 5, 3, G, H]
66. */

12

You might also like