Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java ArrayLists. Write a program that reads 3 integers and displays them for (int i = 0; i < 3; i=i+1) { int val = in.nextInt(); System.out.println(val);

Similar presentations


Presentation on theme: "Introduction to Java ArrayLists. Write a program that reads 3 integers and displays them for (int i = 0; i < 3; i=i+1) { int val = in.nextInt(); System.out.println(val);"— Presentation transcript:

1 Introduction to Java ArrayLists

2 Write a program that reads 3 integers and displays them for (int i = 0; i < 3; i=i+1) { int val = in.nextInt(); System.out.println(val); }

3 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

4 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 int v 1, v 2, …, v n-1,v n v 1 = in.nextInt(); v 2 = in.nextInt(); … v n-1 = in.nextInt(); v n = in.nextInt(); System.out.println(v n ); system.out.println(v n-1 ); … system.out.println(v 1 );

5 ArrayList A sequence of 0 or more values under a common name – Values have the same types – Values are distinguished by their position 1 5 3 2 18 11 0 1 2 3 4 5 a

6 Operations create new ArrayList (); Creates an ArrayList with size 0; type of each value in the ArrayList is Type ArrayList a = new ArrayList (); a

7 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

8 add(x) ArrayList a = new ArrayList (); a.add(1); a.add(5); a.add(3); a.add(2); a.add(18); a.add(11); 1 5 0 1 a a.size() is 2 1 5 3 0 1 2 a a.size() is 3 a a.size() is 01 5 3 2 18 11 0 1 2 3 4 5 a a.size() is 6 a1 a.size() is 1 0

9 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(); 1 5 3 2 18 11 0 1 2 3 4 5 a

10 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(); 1 5 3 2 18 11 0 1 2 3 4 5 a1 5 5 2 18 11 0 1 2 3 4 5 a1 5 5 2 18 15 0 1 2 3 4 5 a

11 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(); 1 5 3 2 18 15 0 1 2 3 4 5 a1 5 2 18 15 0 1 2 3 4 a 5 2 18 15 0 1 2 3 a

12 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 5 2 18 15 0 1 2 3 4 5 a 5 2 18 15 0 1 2 3 4 a 5 2 18 15 0 1 2 3 a 1 3 1 5 3 2 18 15 0 1 2 3 4 5 6 a 12

13 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 numbers = new ArrayList (); File inpFile = new File(“input.txt"); Scanner in = new Scanner(inpFile); while (in.hasNext()) { int val = in.nextInt(); numbers.add(val); } for (int i=numbers.size()-1; i>=0; i=i-1) System.out.println(numbers.get(i));

14 Write a that reads and executes a sequence of commands as follows aread an integer value and add it to the end of an ArrayList ddisplay the content of the ArrayList fread 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 ssort the content of the ArrayList qterminate execution of the program

15 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

16 Exchange 8 2 7 3 6 1 0 1 2 3 4 5 a ijMini a 1 2 7 3 6 8 0 1 2 3 4 5 Don’t exchange ijMin a1 2 7 3 6 8 0 1 2 3 4 5 Exchange

17 1 2 3 7 6 8 0 1 2 3 4 5 a ijMini 0 1 2 3 4 5 a 1 2 3 6 7 8 Don’t exchange a 1 2 3 6 7 8 0 1 2 3 4 5 i = 4 = size(); done!


Download ppt "Introduction to Java ArrayLists. Write a program that reads 3 integers and displays them for (int i = 0; i < 3; i=i+1) { int val = in.nextInt(); System.out.println(val);"

Similar presentations


Ads by Google