Plus
$7.99 / month
- Nemotron Nano 12B VL
- Nemotron Nano 30B
- 20 AI credits/week
Save your current position in the course
Certificates are issued to Unlimited subscribers who finish a course. Your progress is saved either way — upgrade whenever you want yours.
Report a problem with this course via Discord
View available keyboard shortcuts
Reset all section and file completion progress
In the modern digital landscape, data is the fundamental currency of progress. Every time you like a photo on social media, swipe a credit card, or search for a location on a map, you are generating data. For a computer scientist, learning how to collect, store, and process this information is the bridge between writing simple scripts and building powerful, real-world applications.
This unit, Data Collection, explores the technical and ethical dimensions of handling information in Java. We begin with the critical responsibility of the programmer: ensuring that data is used ethically and that algorithms remain unbiased. From there, we dive into the mechanisms of storage—moving from the static efficiency of Arrays to the dynamic flexibility of ArrayLists. We will learn how to pull information from external Text Files and organize complex information into 2D Arrays (matrices). Finally, we will master the logic of Searching and Sorting—the algorithms that allow us to find a needle in a digital haystack. By the end of this chapter, you will transition from a coder who manages variables to a developer who manages systems of information.
java.util package that provides a resizable-array implementation.Integer for int).Programmers act as gatekeepers of information. When we collect data, we must consider:
An array is a linear data structure. Once created, its length is immutable (cannot change).
int[] scores;scores = new int[5]; (Initializes elements to default: $0$ for numbers, false for boolean, null for objects).int[] primes = {2, 3, 5, 7};arr[index].ArrayIndexOutOfBoundsException.We use loops to process arrays.
for(int i = 0; i < arr.length; i++) — Used when we need the index.for(int val : arr) — Used for reading. Warning: You cannot modify the original primitive values in the array using this loop variable because val is a copy.Data persistence allows information to survive after the program closes.
import java.util.Scanner; and import java.io.File;.Scanner fileReader = new Scanner(new File("data.txt"));throws IOException.while(fileReader.hasNext()) to prevent reading past the end of the file.Primitives (int, double, boolean) are not objects. To store them in an ArrayList, Java uses Wrapper classes.
Double d = 5.5; (Converts primitive 5.5 to Double object).int x = Integer.parseInt("123"); converts a String to an int.The ArrayList<E> is dynamic.
size(): Returns number of elements.add(obj): Appends to the end.add(index, obj): Inserts at index, shifts everything else right.set(index, obj): Replaces element at index.remove(index): Deletes and shifts everything else left.A 2D array is a grid. int[][] table = new int[rows][cols];.
table.length = Number of rows.table[0].length = Number of columns.Recursion requires a Base Case (the stopping condition) and a Recursive Call (the part where the function calls itself with a simpler version of the problem).
A company uses an algorithm to screen resumes. The training data only consists of past employees, most of whom were male engineers.
double[] measurements = new double[3];
// measurements[0] is 0.0
// measurements[1] is 0.0
// measurements[2] is 0.0
To find the minimum value in array $A$:
Given an array int[] arr = {10, 20, 30};
$$\text{Sum} = 10 + 20 + 30 = 60$$
$$\text{Average} = \frac{60}{arr.length} = \frac{60}{3} = 20.0$$
int[] data = {1, 2, 3};
System.out.println(data[3]); // ERROR: Indices are 0, 1, 2.
int[] nums = {1, 2, 3};
for (int n : nums) {
n = n * 2; // n is a local copy
}
// nums is still {1, 2, 3}
Rectangle[] rects = {new Rectangle(5, 5)};
for (Rectangle r : rects) {
r.setWidth(10); // This DOES change the object in the array
}
Scanner sc = new Scanner(new File("input.txt"));
while(sc.hasNext()) {
String word = sc.next();
System.out.println(word);
}
sc.close();
String line = "Apple,Banana,Cherry";
String[] fruits = line.split(",");
// fruits[0] is "Apple", fruits[1] is "Banana"...
ArrayList<Integer> list = new ArrayList<>();
list.add(5); // Autoboxing: int 5 -> Integer object
int first = list.get(0); // Unboxing: Integer -> int
// Removing all 2s
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 2, 3));
for(int i = 0; i < list.size(); i++) {
if(list.get(i) == 2) list.remove(i);
}
// Result: [1, 2, 3] because index shifted!
for(int i = list.size() - 1; i >= 0; i--) {
if(list.get(i) == 2) list.remove(i);
}
// Result: [1, 3]
int[][] matrix = { {1, 2}, {3, 4}, {5, 6} };
// matrix.length is 3 (rows)
// matrix[0].length is 2 (columns)
int total = 0;
for(int r = 0; r < matrix.length; r++) {
for(int c = 0; c < matrix[r].length; c++) {
total += matrix[r][c];
}
}
for(int c = 0; c < matrix[0].length; c++) {
for(int r = 0; r < matrix.length; r++) {
System.out.print(matrix[r][c] + " ");
}
}
public int findTarget(int[] arr, int target) {
for(int i = 0; i < arr.length; i++) {
if(arr[i] == target) return i;
}
return -1;
}
Array: [5, 3, 8, 2]
[5, 3, 8, 2]: 2. Swap with 5. Result: [2, 3, 8, 5][3, 8, 5]: 3. Already in place. Result: [2, 3, 8, 5][8, 5]: 5. Swap with 8. Result: [2, 3, 5, 8]Array: [4, 3, 2]
3 is less than 4. Shift 4 right, insert 3: [3, 4, 2]2 is less than 4 and 3. Shift 4, shift 3, insert 2: [2, 3, 4]fact(3):
3 * fact(2)fact(2) calls 2 * fact(1)fact(1) returns 1 (Base case)Target: 7. Array: [1, 3, 5, 7, 9, 11, 13]
arr[3] is 7. Found!int first = arr[0];
for(int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i+1];
}
arr[arr.length - 1] = first;
for(int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
int count = 0;
for(int val : arr) {
if(val % 2 == 0) count++;
}
boolean allEven = true;
for(int val : arr) {
if(val % 2 != 0) {
allEven = false;
break;
}
}
boolean hasDuplicate = false;
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j]) hasDuplicate = true;
}
}
for(int i = 0; i < arr.length - 1; i++) {
int diff = Math.abs(arr[i] - arr[i+1]);
}
Scanner sc = new Scanner(new File("nums.txt"));
double sum = 0;
int count = 0;
while(sc.hasNextDouble()) {
sum += sc.nextDouble();
count++;
}
double avg = sum / count;
int colIndex = 1;
int sum = 0;
for(int r = 0; r < matrix.length; r++) {
sum += matrix[r][colIndex];
}
String val = "45.6";
double d = Double.parseDouble(val);
System.out.println(Integer.MAX_VALUE); // 2147483647
ArrayList<String> names = new ArrayList<>();
names.add("Alex"); // size 1
names.set(0, "Ben"); // size still 1, "Alex" is gone.
[38, 27, 43, 3]
Split: [38, 27] and [43, 3]
Split: [38], [27], [43], [3]
Merge: [27, 38], [3, 43]
Merge: [3, 27, 38, 43]
To find target $x$ in sorted $A$:
$f(low, high)$:
Comparing two arrays of same length:
for(int i = 0; i < arr1.length; i++) {
if(arr1[i] != arr2[i]) return false;
}
A $3 \times 3$ 2D array can be viewed as a 1D array of length $9$.
Element at arr[r][c] corresponds to index i = r * numCols + c.
A medical diagnostic tool is trained on data from adults only.
Right-click any tab and choose Split screen to view it here.
Course Mastery Certificates require an active Unlimited subscription at the time of generation and 100% course completion. They recognise learning content completion only — not an accredited qualification, licence, or professional credential unless explicitly stated. edusolum may modify eligibility, design, and issuance requirements per its Terms of Service.