ArrayListProcessing Program
Description
This program creates an ArrayList of Integer to store a number of integers and then uses several methods to do the following:
Fill in the ArrayList with random numbers
Calculate the total value of all the elements
Find the smallest element in the ArrayList
Search a particular element in the ArrayList
Remove an element at a specific index
Replace an element at a specific index with a new element
Sort the ArrayList
Calculate the total value of all the elements
Find the smallest element in the ArrayList
Search a particular element in the ArrayList
Remove an element at a specific index
Replace an element at a specific index with a new element
Sort the ArrayList
Associated Concepts:
ArrayList
Sequential search algorithm
Selection sort algorithm
Sequential search algorithm
Selection sort algorithm
Algorithm:
main method:
populateArrayList method definition:
printArrayList method definition:
sumArrayList method definition:
indexOfSmallest method definition:
sequentialSearch method definition:
selectionSort method definition:
Create an ArrayList of Integer
Call methods to:
Call methods to:
Populate the array with random integers
Print the elements
Calculate the total value of all the elements
Find the index of the smallest element and print the smallest element
Search an element and print its index if found
Remove the element at index 5 and print the removed element
Replace the element at index 2 with the value of 2247 and print the replaced element
Sort the ArrayList
Print the elements
Print the elements
Calculate the total value of all the elements
Find the index of the smallest element and print the smallest element
Search an element and print its index if found
Remove the element at index 5 and print the removed element
Replace the element at index 2 with the value of 2247 and print the replaced element
Sort the ArrayList
Print the elements
populateArrayList method definition:
Create a Random object
For i = 0 up to 25
For i = 0 up to 25
Generate a random integer and add it to the ArrayList
printArrayList method definition:
For i = 0 up to the size of ArrayList
Print the element at i
sumArrayList method definition:
Declare variable sum and initialize it to 0
For i = 0 up to the size of ArrayList
return sum
For i = 0 up to the size of ArrayList
Add the current element at i to sum
return sum
indexOfSmallest method definition:
Declare variable minIndex and assign it to 0
(Assume the first element is the smallest)
For i = 1 up to the size of ArrayList
return minIndex
For i = 1 up to the size of ArrayList
If the current element at i is less than the smallest element
Update minIndex to the current index i
Update minIndex to the current index i
return minIndex
sequentialSearch method definition:
For i = 0 up to the size of ArrayList
If the loop ended without returning i
Not found and return -1
If the current element at i is equal to the searched key
Found it and return i
Found it and return i
If the loop ended without returning i
Not found and return -1
selectionSort method definition:
Find the smallest element and swap it with the first element
Find the second smallest element and swap it with the second element
Process continue ......
Find the second smallest element and swap it with the second element
Process continue ......