0% found this document useful (0 votes)
2 views5 pages

ArrayList

The document provides examples of using ArrayLists in Java for different data types: Integer, int arrays, and Strings. It demonstrates how to create an ArrayList, add and modify elements, check for containment, find indices, remove elements, and clear the list. Each section includes code snippets and output results to illustrate the functionality of ArrayLists.

Uploaded by

Atiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

ArrayList

The document provides examples of using ArrayLists in Java for different data types: Integer, int arrays, and Strings. It demonstrates how to create an ArrayList, add and modify elements, check for containment, find indices, remove elements, and clear the list. Each section includes code snippets and output results to illustrate the functionality of ArrayLists.

Uploaded by

Atiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ArrayList

Integer Type ArrayList.

1. import java.util.ArrayList;

2. public class ArrayListExample {


3. public static void main(String[] args) {
4. // Create an ArrayList of integers
5. ArrayList<Integer> numbers = new ArrayList<>();

6. // Add elements to the ArrayList


7. numbers.add(10);
8. numbers.add(20);
9. numbers.add(30);

10. // Get the size of the ArrayList


11. int size = numbers.size();
12. System.out.println("Size of ArrayList: " + size);

13. // Accessing elements by index


14. int firstNumber = numbers.get(0);
15. System.out.println("First number: " + firstNumber);

16. // Modifying an element


17. numbers.set(1, 25);
18. System.out.println("Modified ArrayList: " + numbers);

19. // Check if the ArrayList contains an element


20. boolean containsNumber = numbers.contains(30);
21. System.out.println("ArrayList contains 30: " + containsNumber);

22. // Find the index of an element


23. int index = numbers.indexOf(25);
24. System.out.println("Index of 25: " + index);

25. // Remove an element by value


26. boolean removed = numbers.remove(Integer.valueOf(30));
27. System.out.println("Removed 30: " + removed);

28. // Remove an element by index


29. int removedNumber = numbers.remove(0);
30. System.out.println("Removed number: " + removedNumber);

31. // Check if the ArrayList is empty


32. boolean isEmpty = numbers.isEmpty();
33. System.out.println("ArrayList is empty: " + isEmpty);
34. // Clear the ArrayList
35. numbers.clear();
36. System.out.println("Cleared ArrayList: " + numbers);
37. }
38. }
Output:
39. Size of ArrayList: 3
40. First number: 10
41. Modified ArrayList: [10, 25, 30]
42. ArrayList contains 30: true
43. Index of 25: 1
44. Removed 30: true
45. Removed number: 10
46. ArrayList is empty: false
47. Cleared ArrayList: []

Integer-Array type ArrayList.

1. import java.util.ArrayList;

2. public class ArrayListExample {


3. public static void main(String[] args) {
4. // Create an ArrayList of arrays (int[])
5. ArrayList<int[]> numberArrays = new ArrayList<>();

6. // Create and add arrays to the ArrayList


7. int[] array1 = {1, 2, 3};
8. numberArrays.add(array1);

9. int[] array2 = {4, 5, 6};


10. numberArrays.add(array2);

11. // Accessing elements (arrays) by index


12. int[] firstArray = numberArrays.get(0);
13. System.out.print("First array: ");
14. printArray(firstArray);

15. // Modifying an element (array)


16. int[] modifiedArray = {7, 8, 9};
17. numberArrays.set(1, modifiedArray);
18. System.out.print("Modified ArrayList: ");
19. for (int[] array : numberArrays) {
20. printArray(array);
21. }

22. // Checking if the ArrayList contains an element (array)


23. boolean containsArray = numberArrays.contains(array1);
24. System.out.println("ArrayList contains array1: " + containsArray);

25. // Finding the index of an element (array)


26. int index = numberArrays.indexOf(modifiedArray);
27. System.out.println("Index of modifiedArray: " + index);

28. // Removing an element (array) by value


29. boolean removed = numberArrays.remove(array1);
30. System.out.println("Removed array1: " + removed);

31. // Removing an element (array) by index


32. int[] removedArray = numberArrays.remove(0);
33. System.out.print("Removed array: ");
34. printArray(removedArray);

35. // Checking if the ArrayList is empty


36. boolean isEmpty = numberArrays.isEmpty();
37. System.out.println("ArrayList is empty: " + isEmpty);

38. // Clearing the ArrayList


39. numberArrays.clear();
40. System.out.println("Cleared ArrayList: " + numberArrays);
41. }

42. // Helper method to print an array


43. public static void printArray(int[] array) {
44. for (int num : array) {
45. System.out.print(num + " ");
46. }
47. System.out.println();
48. }
49. }
Output:
50. First array: 1 2 3
51. Modified ArrayList: 7 8 9
52. ArrayList contains array1: false
53. Index of modifiedArray: 0
54. Removed array1: true
55. Removed array: 7 8 9
56. ArrayList is empty: true
57. Cleared ArrayList: []

String type ArrayList.

1. import java.util.ArrayList;

2. public class ArrayListExample {


3. public static void main(String[] args) {
4. // Create an ArrayList of strings
5. ArrayList<String> names = new ArrayList<>();

6. // Add elements to the ArrayList


7. names.add("Alice");
8. names.add("Bob");
9. names.add("Charlie");

10. // Get the size of the ArrayList


11. int size = names.size();
12. System.out.println("Size of ArrayList: " + size);

13. // Accessing elements by index


14. String firstElement = names.get(0);
15. System.out.println("First element: " + firstElement);

16. // Modifying an element


17. names.set(1, "Bobby");
18. System.out.println("Modified ArrayList: " + names);

19. // Check if the ArrayList contains an element


20. boolean containsName = names.contains("Charlie");
21. System.out.println("ArrayList contains Charlie: " + containsName);

22. // Find the index of an element


23. int index = names.indexOf("Bobby");
24. System.out.println("Index of Bobby: " + index);

25. // Remove an element by value


26. boolean removed = names.remove("Alice");
27. System.out.println("Removed Alice: " + removed);

28. // Remove an element by index


29. String removedElement = names.remove(0);
30. System.out.println("Removed element: " + removedElement);

31. // Check if the ArrayList is empty


32. boolean isEmpty = names.isEmpty();
33. System.out.println("ArrayList is empty: " + isEmpty);

34. // Clear the ArrayList


35. names.clear();
36. System.out.println("Cleared ArrayList: " + names);
37. }
38. }
Output:
39. Size of ArrayList: 3
40. First element: Alice
41. Modified ArrayList: [Alice, Bobby, Charlie]
42. ArrayList contains Charlie: true
43. Index of Bobby: 1
44. Removed Alice: true
45. Removed element: Alice
46. ArrayList is empty: false
47. Cleared ArrayList: []

You might also like