170 likes | 291 Vues
This guide walks you through writing Java programs that manage integers using ArrayLists. You'll learn to read three integers, display them, and read integers from a file to display them in reverse order. You'll explore the limitations of fixed variables and see how to use ArrayLists for dynamic integer storage. The guide also covers how to read integers, manipulate list content, and sort the ArrayList in ascending order, preparing you for more advanced Java programming tasks.
E N D
Introduction to Java ArrayLists
Write a program that reads 3 integers and displays them for (inti = 0; i < 3; i=i+1) { intval = in.nextInt(); System.out.println(val); }
Write a program that reads integers contained in a file and display them in reverse order • Input: 5 2 76 8 1 • Output: 1 8 76 2 5 • Observations: • Can not start displaying until we have read the last value • The last value must be displayed first • Can not forget any values
int v1, v2, …, vn-1,vn v1= in.nextInt(); v2= in.nextInt(); … vn-1 = in.nextInt(); vn=in.nextInt(); System.out.println(vn); system.out.println(vn-1); … system.out.println(v1); • Write a program that reads integers contained in a file and display them in reverse order • Will not work!! • Don’t know how many variables do we need • Possible solution: • ArrayLists
ArrayList • A sequence of 0 or more values under a common name • Values have the same types • Values are distinguished by their position a 1 5 3 2 18 11 0 1 2 3 4 5
Operations • create new ArrayList<Type>(); Creates an ArrayListwith size 0; type of each value in the ArrayList is Type ArrayList <Integer> a = new ArrayList<Integer>(); a
Operations • add (x) • Increase the size of the ArrayList by 1 • Place value x at the end of ArrayList • size() • Returns the current size of the ArrayList
add(x) a a.size() is 0 ArrayList <Integer> a = new ArrayList<Integer>(); a.add(1); a.add(5); a.add(3); a.add(2); a.add(18); a.add(11); a 1 a.size() is 1 0 a 1 5 a.size() is 2 0 1 a.size() is 3 1 5 3 a a 1 5 3 2 18 11 0 1 2 0 1 2 3 4 5 a.size() is 6
get(i) • return the value at position i of the ArrayList • 0 <= i < size() • a.get(2); returns 3 • a.get(0); returns 1 • a.get(12); • Error: 12 is larger than a.size(); a 1 5 3 2 18 11 0 1 2 3 4 5
set(i,x) • sets the value at position i of the ArrayList to x • 0 <= i < size() • a.set(2, 5); • a.set(5, 15); • a.set(6, 7); • Error: 6 is larger than a.size(); a a a 1 5 3 2 18 11 1 5 5 2 18 11 1 5 5 2 18 15 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
remove(i) • removes the value at position from the ArrayList • 0 <= i < size() • Size of the ArrayList is decreased by 1 • a.remove(2); • a.remove(0); • a.remove(4); • Error: 4 is larger than a.size(); a 1 5 2 18 15 0 1 2 3 4 a 1 5 3 2 18 15 a 5 2 18 15 0 1 2 3 4 5 0 1 2 3
add(i,x) • Increases the size of the ArrayList by 1 • Shifts values at positions i, i+1, … size()-1 to postions i+1, i+2, … size() • The value x is inserted at position i of the ArrayList • 0 <= i<= size() • a.add(0, 1); • a.add(2, 3); • a.add(6, 12); • a.add(8); • Error: 8 is larger than a.size(); 1 a 5 2 18 15 0 1 2 3 4 a 1 5 2 18 15 a 5 2 18 15 3 0 1 2 3 4 5 0 1 2 3 12 a 1 5 3 2 18 15 0 1 2 3 4 5 6
Write a program that reads integers contained in a file, input.txt and display them in reverse order • Input: 5 2 76 8 1 • Output: 1 8 76 2 5 ArrayList<Integer> numbers = new ArrayList<Integer>(); File inpFile = new File(“input.txt"); Scanner in = new Scanner(inpFile); while (in.hasNext()) { intval = in.nextInt(); numbers.add(val); } for (inti=numbers.size()-1; i>=0; i=i-1) System.out.println(numbers.get(i));
Write a that reads and executes a sequence of commands as follows a read an integer value and add it to the end of an ArrayList d display the content of the ArrayList f read an integer value and display its position in the arrayList. If the input value is not in the ArrayList, then display a message r read an integer value and remove it from the ArrayList if the value is not in the ArrayList, display a message s sort the content of the ArrayList q terminate execution of the program
Write a method that sorts the values of an ArrayList in the ascending order • Before sort: 8 2 7 3 6 1 • After sort: 1 2 3 5 6 7 • Step through the positions of the ArrayList one by one positions 0 to size() – 1 • Find the smallest value in the rest of the ArrayList in positions i+1 to size() • If the smallest value is found at position jMin • If value at position i is larger than the value at position jMin • Exchange the two values
a 8 2 7 3 6 1 0 1 2 3 4 5 Exchange Exchange i i i jMin jMin jMin a 1 2 7 3 6 8 0 1 2 3 4 5 Don’t exchange a 1 2 7 3 6 8 0 1 2 3 4 5
a 1 2 3 7 6 8 0 1 2 3 4 5 Exchange i i jMin jMin a 1 2 3 6 7 8 0 1 2 3 4 5 Don’t exchange 1 2 3 6 7 8 a 0 1 2 3 4 5 i = 4 = size(); done!