Arrays and Hash Tables. Counting Scores. Arrays and Array List, Counting Words. Hash Tables
Arrays and Hash Tables. Counting Scores. Arrays and Array List, Counting Words. Hash Tables
Hash
tables.
Arrays
Arrays are fixed-size data structures that hold multiple values of the same type.
In Java, arrays are declared like this: int[] arr = new int[5]; which creates an integer array with 5
elements.
ArrayList
It allows adding and removing elements dynamically without needing to declare a fixed size.
import java.util.Scanner;
// Input scores
scores[i] = scanner.nextInt();
}
// Display scores
System.out.println("Scores of students:");
maxScore = score;
}
Program: Counting Scores Using ArrayList
import java.util.ArrayList;
import java.util.Scanner;
// Input scores
while (true) {
// Display scores
System.out.println("Scores of students:");
maxScore = score;
}
Counting Words Using Hash Tables
A hash table is a data structure that stores key-value pairs and allows fast lookups by hashing
the keys.
In Java, HashMap is used as a hash table implementation where keys and values can be stored
and accessed efficiently.
import java.util.HashMap;
import java.util.Scanner;
System.out.println("Enter a sentence:");
if (wordCounts.containsKey(word)) {
} else {
System.out.println("Word counts:");