P. 1
Collections

Collections

|Views: 7|Likes:
Published by Hema Hrishi

More info:

Published by: Hema Hrishi on May 18, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

07/24/2013

pdf

text

original

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

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

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

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

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

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

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

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

*/ 24. 51. boolean array2[]) 19. boolean[] blnArray2 = new boolean[]{true. Compare Two Java boolean Arrays Example 3. import java. 1 66. It returns true if both arrays are equal. 1 56. Iterating through ArrayList elements in forward direction.. 3 64. 7. This java example shows how to compare two boolean arrays for equality using 4. 21. Arrays. if they contain same elements in same order.. Iterating through ArrayList elements in backward direction. To compare two boolean arrays use..util. Arrays are considered as equal 22. public class CompareBooleanArraysExample { 10. 54.previous()).true}. /* 17. boolean[] blnArray1 = new boolean[]{true."). System. 9. method of Arrays class.. 9 . 18. 20. */ 6. 23. 5 62. //create boolean arrays 13. 4 59. /* 2.out. 4 63. 2 65. direction. 61. while(itr. 2 57. 5 60. 15.Arrays. Output would be 53. static boolean equals(boolean array1[]. 8. } 49. 45. 55. 5..false. 16. } 50.true}. 11. 14.. public static void main(String[] args) { 12. Iterating through ArrayList elements. 47.false. */ Compare Two Java boolean Arrays Example 1. /* 52. 3 58. 48.hasPrevious()) 46..equals method..44.println(itr.

This java example shows how to compare two byte arrays for equality using 43. byte[] byteArray1 = new byte[]{7. 56. byte[] byteArray2 = new byte[]{7. Output of the program would be 38. /* 29. public static void main(String[] args) { 50. 27. 63. */ 69. To compare two byte arrays use.25. /* 37.out.Arrays. 54. 64.12}.println("Are two byte arrays equal ? : " + blnResult). */ 40. 53.println("Are two boolean arrays equal ? : " + blnResult). 36. Are two boolean arrays equal ? : true 39. Compare Two Java byte Arrays Example 42.out. boolean blnResult = Arrays. 26.12}. Please note that two boolean array references pointing to null are 30.util.equals(byteArray1.blnArray2). /* 66. Please note that two byte array references pointing to null are 67. 62.25. 46.equals(blnArray1. considered as equal. /* 55. 57. } 34. 47. It returns true if both arrays are equal. 28. 49.byteArray2). System. 65. 31. */ 32. 60. */ 61. if they contain same elements in same order. System. import java. 58. Arrays. static boolean equals(byte array1[]. 52. boolean blnResult = Arrays. considered as equal.equals method.25. Arrays are considered as equal 59. 44. byte array2[]) method of Arrays class. */ Compare Two Java byte Arrays Example 45. 68. //create byte arrays 51. public class CompareByteArraysExample { 48. 33. } 35. /* 41. 10 .

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

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

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->