ArrayLists of Data10.0
Just like arrays, an ArrayList is an (abstract) data structure which may be employed towards aggregating objects or values of a specific Non-Primitive data type. However, in opposition to arrays which possess a fixed size after its declaration and instantiation,
ArrayLists are dynamic with respect to their sizes such that they can dynamically grow and adjust to accommodate newer objects into the ArrayList. Arrays as well as ArrayLists are both used to facilitate effective and efficient data management with respect to their respective aggregated objects or values.
Therefore, some important points to note with regard to ArrayLists in Java include the following:
- Prior to implementing an ArrayList in Java, the
ArrayList
class block resident in the java.util
package must be imported via: import java.util.ArrayList;
- An ArrayList is a mutable collection of objects (or Non-Primitive data values).
- In Java, an ArrayList can only be used to store objects or values of Non-Primitive (data) type such as
Integer
, Float
, Boolean
, String
, Object
, etc.
- The size of an ArrayList does not need to be explicitly specified during its declaration and/or instantiation.
- After the declaration and/or instantiation of an ArrayList; by default, each cell or unit contains a
null
value because only objects (or Non-Primitive values) can reside in an ArrayList.
- An ArrayList's size is dynamic during the runtime of a program; such that its elasticity can expand as much as possible to accommodate newer objects during the execution of a program.
- Each object, resident in an ArrayList, occupies a cell/unit position with respect to the ArrayList's size.
- Also, each cell/unit possesses an integer-based index which begins from zero (0) at the top/head of the ArrayList structure.
- In other words, ArrayList indexing begins at zero (0) with reference to the dynamic size of the ArrayList.
Caption: Simplified ArrayList (data) structure comprising several cells/units labelled with respective indexes
Thus, the following code snippet showcases a couple of approaches for declaring and instantiating (or implementating) a one-dimensional (1D) ArrayList data structure in Java programming:
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList - Approach 1
ArrayList<Float> newFloatArrList;
newFloatArrList = new ArrayList<Float>();
//Declaration and Instantiation of one-dimensional (1D) ArrayList - Approach 2
ArrayList<Double> newDoubleArrList = new ArrayList<Double>();
}
}
Code Snippet: Declaration and instantiation of one-dimensional (1D) ArrayLists data structure
Furthermore, the following code snippet illustrates a couple of handy methods (or functions), which have already been implemented within the
ArrayList
class block, for the manipulation and management of ArrayLists.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<Integer> newIntegerArrList = new ArrayList<Integer>();
//Manipulating ArrayList: Addition of objects/items
newIntegerArrList.add(0); //Add object to index 0 (index implicitly created by ArrayList)
newIntegerArrList.add(1, 100); //Add object to index 1 (index explicitly specified by user)
newIntegerArrList.add(2, -5); //Add object to index 2 (index explicitly specified by user)
newIntegerArrList.add(1); //Add object to index 3 (index implicitly created by ArrayList), and so on ...
newIntegerArrList.add(4);
newIntegerArrList.add(5, 10);
newIntegerArrList.add(8);
newIntegerArrList.add(9);
newIntegerArrList.add(5);
newIntegerArrList.add(41);
System.out.println("After addition of objects: " + newIntegerArrList + "\n");
//Manipulating ArrayList: Removal of objects/items
newIntegerArrList.remove(0); //Remove object at index 0
newIntegerArrList.remove(8); //Remove object at index 8
System.out.println("After removal of objects: " + newIntegerArrList + "\n");
//Manipulating ArrayList: Update to objects/items
newIntegerArrList.set(2, 45); //Update object at index 2 = 45
newIntegerArrList.set(7, 500); //Update object at index 7 = 500
newIntegerArrList.set(5, 45); //Update object at index 5 = 45
System.out.println("After update to objects: " + newIntegerArrList + "\n");
//Manipulating ArrayList: Return of objects/items
Integer obj1 = newIntegerArrList.get(2); //Return object at index 2
System.out.println("Value of object at index 2 = " + obj1);
Integer obj2 = newIntegerArrList.get(0); //Return object at index 0
System.out.println("Value of object at index 0 = " + obj2);
Integer obj3 = newIntegerArrList.get(1); //Return object at index 1
System.out.println("Value of object at index 1 = " + obj3 + "\n");
//Manipulating ArrayList: Size of ArrayList
int sizeOfArrayList = newIntegerArrList.size(); //Computes the size/length of the ArrayList
System.out.println("Size of the ArrayList = " + sizeOfArrayList + " objects/items\n");
//Manipulating ArrayList: Traversal of ArrayList using "for () {...}" loop construct
for (int i = 0; i < sizeOfArrayList; i++) {
System.out.println("Object/Item at index " + i + " = " + newIntegerArrList.get(i));
}
System.out.println();
//Manipulating ArrayList: Traversal of ArrayList using "for-each () {...}" loop construct
int idx = 0;
for (Integer item : newIntegerArrList) {
System.out.println("Object/Item at index " + idx++ + " = " + item);
}
}
}
Code Snippet: Accessing, manipulating, and managing the objects of a 1D ArrayList via its respective index(es)
10.1Shallow Copy and Deep Copy in 1D ArrayLists
In the previous lesson or chapter, we examined the concepts of
Shallow Copy and
Deep Copy with reference to arrays. Therefore, in this lesson and/or chapter, we shall also be reviewing the joint concepts of
Shallow Copy and
Deep Copy as it relates to ArrayLists data structure.
Shallow Copy: This is a data-copy technique that copies the object-reference of one ArrayList into another ArrayList.
Deep Copy: Alternatively, this is a data-copy technique which copies the actual data content of one ArrayList into another ArrayList. There exist several approaches in Java programming for implementing the concept of
Deep Copy with regard to ArrayList;
and these approaches are illustrated in the following blocks of Java program.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<Integer> newIntegerArrList = new ArrayList<Integer>();
//Manipulating ArrayList: Addition of objects/items
newIntegerArrList.add(0);
newIntegerArrList.add(100);
newIntegerArrList.add(-5);
newIntegerArrList.add(1);
newIntegerArrList.add(4);
newIntegerArrList.add(10);
newIntegerArrList.add(8);
newIntegerArrList.add(9);
newIntegerArrList.add(5);
newIntegerArrList.add(41);
//Shallow Copy (object-reference copied)
ArrayList<Integer> newIntegerArrList1 = newIntegerArrList;
//Deep Copy (actual data-content copied)
ArrayList<Integer> newIntegerArrList2 = new ArrayList<Integer>();
for (int i = 0; i < newIntegerArrList.size(); i++) {
newIntegerArrList2.add(i, newIntegerArrList.get(i));
}
newIntegerArrList.set(0, 1290); //Update to only index 0 of 'newIntegerArrList<>'
newIntegerArrList.set(5, 5384); //Update to only index 5 of 'newIntegerArrList<>'
System.out.println("newIntegerArrList<>: " + newIntegerArrList);
System.out.println("newIntegerArrList1<>: " + newIntegerArrList1);
System.out.println("newIntegerArrList2<>: " + newIntegerArrList2);
}
}
Code Snippet: Deep Copy (and Shallow Copy) with regard to one-dimensional ArrayList (Approach 1)
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<Integer> newIntegerArrList = new ArrayList<Integer>();
//Manipulating ArrayList: Addition of objects/items
newIntegerArrList.add(0);
newIntegerArrList.add(100);
newIntegerArrList.add(-5);
newIntegerArrList.add(1);
newIntegerArrList.add(4);
newIntegerArrList.add(10);
newIntegerArrList.add(8);
newIntegerArrList.add(9);
newIntegerArrList.add(5);
newIntegerArrList.add(41);
//Shallow Copy (object-reference copied)
ArrayList<Integer> newIntegerArrList1 = newIntegerArrList;
//Deep Copy (actual data-content copied)
ArrayList<Integer> newIntegerArrList2 = new ArrayList<Integer>(newIntegerArrList);
newIntegerArrList.set(0, 1290); //Update to only index 0 of 'newIntegerArrList<>'
newIntegerArrList.set(5, 5384); //Update to only index 5 of 'newIntegerArrList<>'
System.out.println("newIntegerArrList<>: " + newIntegerArrList);
System.out.println("newIntegerArrList1<>: " + newIntegerArrList1);
System.out.println("newIntegerArrList2<>: " + newIntegerArrList2);
}
}
Code Snippet: Deep Copy (and Shallow Copy) with regard to one-dimensional ArrayList (Approach 2)
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<Integer> newIntegerArrList = new ArrayList<Integer>();
//Manipulating ArrayList: Addition of objects/items
newIntegerArrList.add(0);
newIntegerArrList.add(100);
newIntegerArrList.add(-5);
newIntegerArrList.add(1);
newIntegerArrList.add(4);
newIntegerArrList.add(10);
newIntegerArrList.add(8);
newIntegerArrList.add(9);
newIntegerArrList.add(5);
newIntegerArrList.add(41);
//Shallow Copy (object-reference copied)
ArrayList<Integer> newIntegerArrList1 = newIntegerArrList;
//Deep Copy (actual data-content copied)
ArrayList<Integer> newIntegerArrList2 = (ArrayList<Integer>) newIntegerArrList.clone(); //WARNING = "Type safety: Unchecked cast"
newIntegerArrList.set(0, 1290); //Update to only index 0 of 'newIntegerArrList<>'
newIntegerArrList.set(5, 5384); //Update to only index 5 of 'newIntegerArrList<>'
System.out.println("newIntegerArrList<>: " + newIntegerArrList);
System.out.println("newIntegerArrList1<>: " + newIntegerArrList1);
System.out.println("newIntegerArrList2<>: " + newIntegerArrList2);
}
}
Code Snippet: Deep Copy (and Shallow Copy) with regard to one-dimensional ArrayList (Approach 3)
10.2Two-Dimensional (2D) ArrayLists
Similar to arrays, it is also feasible to create multi-dimensional ArrayLists in Java. Hence, the following code snippets illustrate how to declare and instantiate a two-dimensional (2D) ArrayList.
Additional useful methods (or functions), which have already been implemented within the
ArrayList
class block, for the manipulation and management of 2D ArrayLists have also been exemplified in this section.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of two-dimensional (2D) ArrayList - Approach 1
ArrayList<ArrayList<Float>> newFloatArrList;
newFloatArrList = new ArrayList<ArrayList<Float>>();
//Declaration and Instantiation of two-dimensional (2D) ArrayList - Approach 2
ArrayList<ArrayList<String>> newStringArrList = new ArrayList<ArrayList<String>>();
}
}
Code Snippet: Declaration and instantiation of two-dimensional (2D) ArrayLists data structure
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of two-dimensional (2D) ArrayList
ArrayList<ArrayList<String>> newStringArrList = new ArrayList<ArrayList<String>>(); //Instantiates row-wise dimension
//Manipulating 2D ArrayList: Addition of objects/items
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(0).add("Alpha"); //Add object to index 0,0 (index implicitly created by ArrayList)
newStringArrList.get(0).add(1, "Bravo"); //Add object to index 0,1 (index explicitly specified by user)
newStringArrList.get(0).add(2, "Charlie");
newStringArrList.get(0).add("Delta");
newStringArrList.get(0).add("Echo");
newStringArrList.get(0).add(5, "Foxtrot");
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(1).add("Golf"); //Add object to index 1,0 (index implicitly created by ArrayList), and so on ...
newStringArrList.get(1).add("Hotel");
newStringArrList.get(1).add("India");
newStringArrList.get(1).add("Juliett");
System.out.println("After addition of objects: " + newStringArrList + "\n");
//Manipulating 2D ArrayList: Removal of objects/items
newStringArrList.get(0).remove(0); //Remove object at index 0,0
newStringArrList.get(1).remove(2); //Remove object at index 1,2
System.out.println("After removal of objects: " + newStringArrList + "\n");
//Manipulating 2D ArrayList: Update to objects/items
newStringArrList.get(0).set(0, "Quebec"); //Update object at index 0,0 = "Quebec"
newStringArrList.get(1).set(1, "Zulu"); //Update object at index 1,1 = "Zulu"
newStringArrList.get(1).set(2, "Yankee"); //Update object at index 1,2 = "Yankee"
System.out.println("After update to objects: " + newStringArrList + "\n");
//Manipulating 2D ArrayList: Return of objects/items
String obj1 = newStringArrList.get(0).get(0); //Return object at index 0,0
System.out.println("Value of object at index 0,0 = " + obj1);
String obj2 = newStringArrList.get(1).get(1); //Return object at index 1,1
System.out.println("Value of object at index 1,1 = " + obj2);
String obj3 = newStringArrList.get(0).get(4); //Return object at index 0,4
System.out.println("Value of object at index 0,4= " + obj3 + "\n");
//Manipulating 2D ArrayList: Size of ArrayList
int rowSizeArrayList = newStringArrList.size(); //Computes the row-size of the ArrayList
System.out.println("Rows' size of the ArrayList = " + rowSizeArrayList + " objects/items\n");
//Manipulating 2D ArrayList: Traversal of ArrayList using "for () {...}" loop construct
for (int i = 0; i < rowSizeArrayList; i++) {
for (int j = 0; j < newStringArrList.get(i).size(); j++) {
System.out.println("Object/Item at index " + i + "," + j + " = " + newStringArrList.get(i).get(j));
}
}
System.out.println();
//Manipulating 2D ArrayList: Traversal of ArrayList using "for-each () {...}" loop construct
int rw = 0;
for (ArrayList<String> row : newStringArrList) {
int cl = 0;
for (String obj : row) {
System.out.println("Object/Item at index " + rw + "," + cl++ + " = " + obj);
}
rw++;
}
}
}
Code Snippet: Accessing, manipulating, and managing the elements of a 2D ArrayList via its respective index(es)
10.3Shallow Copy and Deep Copy in 2D ArrayLists
The following code snippets illustrate the concepts of Shallow Copy and Deep Copy with reference to two-dimensional (2D) ArrayLists in Java.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of two-dimensional (2D) ArrayList
ArrayList<ArrayList<String>> newStringArrList = new ArrayList<ArrayList<String>>(); //Instantiates row-wise dimension
//Manipulating 2D ArrayList: Addition of objects/items
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(0).add("Alpha");
newStringArrList.get(0).add(1, "Bravo");
newStringArrList.get(0).add(2, "Charlie");
newStringArrList.get(0).add("Delta");
newStringArrList.get(0).add("Echo");
newStringArrList.get(0).add(5, "Foxtrot");
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(1).add("Golf");
newStringArrList.get(1).add("Hotel");
newStringArrList.get(1).add("India");
newStringArrList.get(1).add("Juliett");
//Shallow Copy (object-reference copied) - Approach 1
ArrayList<ArrayList<String>> newStringArrList1 = newStringArrList;
//Shallow Copy (object-reference copied) - Approach 2
ArrayList<ArrayList<String>> newStringArrList2 = new ArrayList<ArrayList<String>>(newStringArrList);
newStringArrList.get(0).set(0, "Zulu"); //Update object at index 0,0 of 'newStringArrList<<>>'
newStringArrList.get(0).set(5, "Quebec"); //Update object at index 0,5 of 'newStringArrList<<>>'
newStringArrList.get(1).set(0, "Yankee"); //Update object at index 1,0 of 'newStringArrList<<>>'
newStringArrList.get(1).set(3, "Romeo"); //Update object at index 1,3 of 'newStringArrList<<>>'
System.out.println("newStringArrList<<>>: " + newStringArrList);
System.out.println("newStringArrList1<<>>: " + newStringArrList1);
System.out.println("newStringArrList2<<>>: " + newStringArrList2);
}
}
Code Snippet: Shallow Copy with regard to two-dimensional ArrayList (Approaches 1 and 2)
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of two-dimensional (2D) ArrayList
ArrayList<ArrayList<String>> newStringArrList = new ArrayList<ArrayList<String>>(); //Instantiates row-wise dimension
//Manipulating 2D ArrayList: Addition of objects/items
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(0).add("Alpha");
newStringArrList.get(0).add(1, "Bravo");
newStringArrList.get(0).add(2, "Charlie");
newStringArrList.get(0).add("Delta");
newStringArrList.get(0).add("Echo");
newStringArrList.get(0).add(5, "Foxtrot");
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(1).add("Golf");
newStringArrList.get(1).add("Hotel");
newStringArrList.get(1).add("India");
newStringArrList.get(1).add("Juliett");
//Deep Copy (actual data-content copied) - Approach 1
ArrayList<ArrayList<String>> newStringArrList1 = new ArrayList<ArrayList<String>>();
for (int i = 0; i < newStringArrList.size(); i++) {
newStringArrList1.add(new ArrayList<String>());
for (int j = 0; j < newStringArrList.get(i).size(); j++) {
newStringArrList1.get(i).add(j, newStringArrList.get(i).get(j));
}
}
//Deep Copy (actual data-content copied) - Approach 2
ArrayList<ArrayList<String>> newStringArrList2 = new ArrayList<ArrayList<String>>();
for (int i = 0; i < newStringArrList.size(); i++) {
newStringArrList2.add(new ArrayList<String>(newStringArrList.get(i)));
}
newStringArrList.get(0).set(0, "Zulu"); //Update object at index 0,0 of 'newStringArrList<<>>'
newStringArrList.get(0).set(5, "Quebec"); //Update object at index 0,5 of 'newStringArrList<<>>'
newStringArrList.get(1).set(0, "Yankee"); //Update object at index 1,0 of 'newStringArrList<<>>'
newStringArrList.get(1).set(3, "Romeo"); //Update object at index 1,3 of 'newStringArrList<<>>'
System.out.println("newStringArrList<<>>: " + newStringArrList);
System.out.println("newStringArrList1<<>>: " + newStringArrList1);
System.out.println("newStringArrList2<<>>: " + newStringArrList2);
}
}
Code Snippet: Deep Copy with regard to two-dimensional ArrayList (Approaches 1 and 2)
10.4Implementation of Methods (or Functions) in Java
Consequently, and just like arrays, ArrayLists can be declared as parameter(s) in a method. In that regard, when ArrayList(s) is/are employed as parameter(s) in a method; its corresponding argument(s) are always "Passed by Reference". Also, a method (or function) can be defined such that it returns an ArrayList.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<Integer> newIntegerArrList = new ArrayList<Integer>();
//Manipulating ArrayList: Addition of objects/items
newIntegerArrList.add(0);
newIntegerArrList.add(100);
newIntegerArrList.add(-5);
newIntegerArrList.add(1);
newIntegerArrList.add(4);
newIntegerArrList.add(10);
newIntegerArrList.add(8);
newIntegerArrList.add(9);
newIntegerArrList.add(5);
newIntegerArrList.add(41);
int arrListSize = newIntegerArrList.size();
//Create an instance/object of the (host) class block
ArrayListsofData obj1 = new ArrayListsofData();
int sumRes = ArrayListsofData.summation(newIntegerArrList); //Argument = "newIntegerArrList<>" (Pass by Reference)
obj1.outputStr(arrListSize, sumRes); //Arguments = "arrListSize", "sumRes" (Pass by Value)
}
//Instance method
public void outputStr (int arrListLen, int arrSum) { //Parameters = "arrListLen", "arrSum" (Pass by Value)
System.out.println("The sum of the " + arrListLen + " objects resident in the ArrayList is = " + arrSum);
}
//Static method
public static int summation (ArrayList<Integer> arrListRef) { //Parameter = "arrListRef<>" (Pass by Reference)
int sum = 0;
for (Integer obj : arrListRef) {
sum += obj; //Accessing the true value of "arrListRef<>"
}
return sum;
}
}
Code Snippet: Pass by Reference and Pass by Value to methods/functions
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
//Declaration and Instantiation of one-dimensional (1D) ArrayList
ArrayList<String> newStringArrList = new ArrayList<String>();
//Manipulating ArrayList: Addition of objects/items
newStringArrList.add("Alpha");
newStringArrList.add("Bravo");
newStringArrList.add("Charlie");
newStringArrList.add("Delta");
newStringArrList.add("Echo");
newStringArrList.add("Foxtrot");
newStringArrList.add("Golf");
//Call to a static method of the (host) class block
ArrayList<String> newStringArrList1 = ArrayListsofData.reverse(newStringArrList); //Argument = "newStringArrList<>" (Pass by Reference)
System.out.println(newStringArrList1);
}
//Static method
public static ArrayList<String> reverse(ArrayList<String> values) { //Parameter = "values<>" (Pass by Reference)
ArrayList<String> res = new ArrayList<String>();
for (int i = values.size() - 1; i >= 0; i--) {
res.add(values.get(i));
}
return res;
}
}
Code Snippet: Passing an ArrayList (as argument) and returning an ArrayList from a method/function
Finally, virtually every data management procedure implemented with conventional arrays can also be implemented with ArrayLists, except for the fact that entries into an ArrayList must always be an object (Non-Primitive) type. In this regard, every Primitive (data) type possesses a corresponding Non-Primitive wrapper class with reference to Java programming;
and these we have already discussed in the Lesson (or Chapter) focused on the
Fundamentals of Java. Moreover, additional implementation of methods where ArrayLists could be employed as equivalent (data) management structure can be found in the previous lesson on
Arrays.
10.5Practice Exercise
-
Given the program below, kindly refactor the lines of code herein such that an ArrayList structure is employed in lieu of any existent array (data) structure.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class ArrayListsofData {
public static void main(String[] args) {
int counter = 0;
double cscScores[] = {20.0, 15.0, 0.0, 8, 0};
for (double item : cscScores) {
if (item == 0.0) {
counter++;
}
}
System.out.println("Value of 'counter' is = " + counter);
}
}
-
Given the following Java program; kindly describe what this program does,
and refactor the lines of code herein such that an ArrayList data structure replaces any occurrence of a data array within the program.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
import java.util.Arrays; //package required for the toString() method
public class ArrayListsofData {
public static void main(String[] args) {
int iterator = 0;
final int NUMBER_OF_INPUTS = 3;
double[] inVals = new double[NUMBER_OF_INPUTS];
Scanner usrInput = new Scanner(System.in);
while (iterator < NUMBER_OF_INPUTS) {
System.out.print("Please enter a value for number " + ++iterator + ": ");
inVals[iterator-1] = usrInput.nextDouble();
System.out.println();
}
//toString(): Returns (data) contents of a 1D array in human-friendly format
System.out.println(Arrays.toString(inVals));
//Flushes and closes the "Scanner" buffer so as to prevent resource leakage/wastage.
usrInput.close();
}
}
-
Consider the following Java program with respect to the implementation of a popular algorithm.
Thus, do you think that this program actualizes its goal? If yes, then refactor the lines of code herein such that
it employs an ArrayList (data) structure in lieu of any (data) array structure.
If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class ArrayListsofData {
public static void main(String[] args) {
int dataArr[] = {0, 100, -5, 1, 4, 10, 8, 9, 5, 41};
boolean found = false;
int low = 0, high = (dataArr.length -1), mid = 0, searchValue = 8;
Arrays.sort(dataArr);
while ((low <= high) && !found) {
mid = (low + high) / 2; //Division of integers returns a truncated (or floored) result
if (dataArr[mid] == searchValue) {
found = true; //Found a match
}
else if (dataArr[mid] < searchValue) {
low = mid + 1; //Exclude the lower halve of the array
}
else {
high = mid - 1; //Exclude the upper halve of the array
}
}
if (found) {
System.out.println(searchValue + " found at index " + mid + " of the sorted array.");
}
else {
System.out.print(searchValue + " not found in the entire sorted array. ");
System.out.println("You may wish to insert if before index " + mid + " of the sorted array.");
}
}
}
-
Consider the following Java program with respect to the implementation of an ArrayList. Thus, what will be the size of the ArrayList after the addition of the object: "Charlie"?
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
ArrayList<String> newStringArrList = new ArrayList<String>();
newStringArrList.add("Alpha");
newStringArrList.add(1, "Bravo");
newStringArrList.add("Charlie");
}
}
-
Given the following Java program with respect to the implementation of an ArrayList. Thus, what will be the row-size and column-size of the ArrayList after the addition of the object: "Zulu"?
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
ArrayList<ArrayList<String>> newStringArrList = new ArrayList<ArrayList<String>>(); //Instantiates row-wise dimension
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(0).add("Alpha");
newStringArrList.get(0).add(1, "Bravo");
newStringArrList.get(0).add(2, "Charlie");
newStringArrList.get(0).add("Delta");
newStringArrList.add(new ArrayList<String>()); //Instantiate column-wise dimension per row of ArrayList
newStringArrList.get(1).add("Echo");
newStringArrList.get(1).add("Foxtrot");
newStringArrList.get(1).add("Golf");
newStringArrList.get(1).add("Zulu");
}
}
-
Consider the following Java program with respect to the implementation of an ArrayList. Thus, what will be the sizes of the ArrayLists (
newStringArrList
and arrList
) at the end of the program?
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title ArrayLists of Data.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.ArrayList;
public class ArrayListsofData {
public static void main(String[] args) {
ArrayList<String> newStringArrList = new ArrayList<String>();
newStringArrList.add("Alpha");
newStringArrList.add(1, "Bravo");
newStringArrList.add("Charlie");
ArrayList<String> arrList = newStringArrList;
arrList.add("X-ray");
arrList.add("Zulu");
}
}
- TRUE or FALSE: Passing an ArrayList as an argument into method does so via "Pass by Value"?
- TRUE or FALSE: With respect to Java programming, after the declaration and instantiation of an ArrayList, its default values are directly dependent on the (data) type of the ArrayList?
- TRUE or FALSE: An ArrayList can store values of (data) type:
int
, double
, and byte
?
- TRUE or FALSE: In Java programming, the dimension(s) of an ArrayList must be explicitly specified during its declaration and/or instantiation?