1 / 17

Introduction to Java

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 ); }. Write a program that reads integers contained in a file and display them in reverse order

iolana
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java ArrayLists

  2. 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); }

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

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

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

  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) 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

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

  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(); 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

  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(); 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

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

  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<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));

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

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

  17. 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!

More Related