Introduction to Java ArrayLists. Write a program that reads 3 integers and displays them for (int i...
-
Upload
anne-lindsey -
Category
Documents
-
view
213 -
download
1
Transcript of Introduction to Java ArrayLists. Write a program that reads 3 integers and displays them for (int i...
![Page 1: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/1.jpg)
Introduction to Java
ArrayLists
![Page 2: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/2.jpg)
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);
}
![Page 3: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/3.jpg)
• 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
![Page 4: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/4.jpg)
• 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 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);
![Page 5: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/5.jpg)
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
![Page 6: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/6.jpg)
Operations
• createnew ArrayList<Type>();
Creates an ArrayList with size 0;type of each value in the ArrayList is Type
ArrayList <Integer> a = new ArrayList<Integer>();a
![Page 7: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/7.jpg)
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
![Page 8: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/8.jpg)
add(x)ArrayList <Integer> a = new ArrayList<Integer>();
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 0
1 5 3 2 18 11 0 1 2 3 4 5
a a.size() is 6
a 1 a.size() is 1 0
![Page 9: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/9.jpg)
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
![Page 10: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/10.jpg)
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
a
1 5 5 2 18 11 0 1 2 3 4 5
a
1 5 5 2 18 15 0 1 2 3 4 5
a
![Page 11: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/11.jpg)
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
a
1 5 2 18 15 0 1 2 3 4
a
5 2 18 15 0 1 2 3
a
![Page 12: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/12.jpg)
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
![Page 13: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/13.jpg)
• 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()) { int val = in.nextInt(); numbers.add(val); } for (int i=numbers.size()-1; i>=0; i=i-1) System.out.println(numbers.get(i));
![Page 14: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/14.jpg)
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 ArrayListif the value is not in the ArrayList, display a message
s sort the content of the ArrayList
q terminate execution of the program
![Page 15: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/15.jpg)
• 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 onepositions 0 to size() – 1
• Find the smallest value in the rest of the ArrayListin 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
![Page 16: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/16.jpg)
Exchange
8 2 7 3 6 1 0 1 2 3 4 5
a
i jMin
i jMin
a 1 2 7 3 6 8 0 1 2 3 4 5
Don’t exchange
i jMin
a 1 2 7 3 6 8 0 1 2 3 4 5
Exchange
![Page 17: 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);](https://reader036.fdocuments.in/reader036/viewer/2022083006/56649f335503460f94c5097f/html5/thumbnails/17.jpg)
Exchange
1 2 3 7 6 8 0 1 2 3 4 5
a
i jMin
i jMin 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!