1 / 12

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

mcribb
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 v1= in.nextInt(); v2= 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

  5. ArrayList • A sequence of 0 or more values under a common name • Values of common 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 ArrayList of type Type with size 0; ArrayList <Integer> a = new ArrayList<Integer>();

  7. Operations • add (x) • Increase the size of the ArrayList by 1 • Place value x at the end of ArrayList

  8. Operations • int size() • return the size of the ArrayList

  9. Operations • get (i) • return the value at position i of the ArrayList • i < size();

  10. Operations • set(i, x) • sets the value at position i of the ArrayList to x • i < size();

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

  12. Write a program that reads integers contained in a file and display them in Ascending order • Input: 5 2 76 8 1 • Output: 1 2 5 8 76

More Related