0% found this document useful (0 votes)
13 views7 pages

Arrays and Hash Tables. Counting Scores. Arrays and Array List, Counting Words. Hash Tables

Uploaded by

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

Arrays and Hash Tables. Counting Scores. Arrays and Array List, Counting Words. Hash Tables

Uploaded by

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

7. Arrays and Hash Tables. Counting scores. Arrays and Array list, Counting words.

Hash
tables.

Arrays and ArrayList

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

 ArrayList is a resizable array-like structure in Java.

 It allows adding and removing elements dynamically without needing to declare a fixed size.

Program: Counting Scores Using Arrays

This program keeps track of the scores of 5 students using arrays.

import java.util.Scanner;

public class ScoreCounter {

public static void main(String[] args) {

// Array to store scores of 5 students

int[] scores = new int[5];

Scanner scanner = new Scanner(System.in);

// Input scores

System.out.println("Enter scores for 5 students:");

for (int i = 0; i < scores.length; i++) {

System.out.print("Enter score for student " + (i + 1) + ": ");

scores[i] = scanner.nextInt();

}
// Display scores

System.out.println("Scores of students:");

for (int i = 0; i < scores.length; i++) {

System.out.println("Student " + (i + 1) + ": " + scores[i]);

// Find the highest score

int maxScore = scores[0];

for (int score : scores) {

if (score > maxScore) {

maxScore = score;

System.out.println("Highest score: " + maxScore);

}
Program: Counting Scores Using ArrayList

This program keeps track of student scores dynamically using an ArrayList.

import java.util.ArrayList;

import java.util.Scanner;

public class ScoreCounterArrayList {

public static void main(String[] args) {

// ArrayList to store scores dynamically

ArrayList<Integer> scores = new ArrayList<>();

Scanner scanner = new Scanner(System.in);

// Input scores

System.out.println("Enter scores (type -1 to stop):");

while (true) {

int score = scanner.nextInt();

if (score == -1) break;


scores.add(score);

// Display scores

System.out.println("Scores of students:");

for (int i = 0; i < scores.size(); i++) {

System.out.println("Student " + (i + 1) + ": " + scores.get(i));

// Find the highest score

int maxScore = scores.get(0);

for (int score : scores) {

if (score > maxScore) {

maxScore = score;

System.out.println("Highest score: " + maxScore);

}
Counting Words Using Hash Tables

Hash Tables (HashMap in Java)

 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.

Program: Counting Words in a Sentence Using HashMap

This program counts the occurrence of each word in a given sentence.

import java.util.HashMap;

import java.util.Scanner;

public class WordCounter {

public static void main(String[] args) {

// Create a HashMap to store word counts

HashMap<String, Integer> wordCounts = new HashMap<>();

Scanner scanner = new Scanner(System.in);


// Input sentence

System.out.println("Enter a sentence:");

String sentence = scanner.nextLine();

// Split sentence into words

String[] words = sentence.split(" ");

// Count the occurrences of each word

for (String word : words) {

word = word.toLowerCase(); // Convert to lowercase for uniform counting

if (wordCounts.containsKey(word)) {

wordCounts.put(word, wordCounts.get(word) + 1); // Increment count if word exists

} else {

wordCounts.put(word, 1); // Add new word with count 1

// Display the word counts

System.out.println("Word counts:");

for (String word : wordCounts.keySet()) {

System.out.println(word + ": " + wordCounts.get(word));

You might also like