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
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
Write a program that reads integers contained
in a file and display them in reverse order
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);
Will not work!!
Dont 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
18
0
4
5
11
1
5
Operations
create
new ArrayList<Type>();
Creates an ArrayList with size 0;
type of each value in the ArrayList is
Type
ArrayList <Integer>
a = new
a
ArrayList<Integer>();
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)
ArrayList <Integer> a = new ArrayList<Integer>();
a
a.size() is 0
a.add(1);
1
0
a.size() is 1
a.add(5);
a.size() is 2
a.add(3);
1
50
1
1
3
0
2
a.add(2);
a.add(18);
a.add(11); a
1
18
0
4
a.size() is 3
5
1
5
11
1
5
3
2
2
3
a.size() is 6
get(i)
return the value at position i of the
ArrayList
0 <= i < size()
a
1
18
0
4
5
11
1
5
3
2
2
3
a.get(2); returns 3
a.get(0); returns 1
a.get(12);
Error: 12 is larger than a.size();
set(i,x)
sets the value at position i of the ArrayList to
x
0 <= i < size()
a
1
18
0
4
a.set(2, 5);
a.set(5, 15);
5
11
1
5
a
3
2
2
3
1
18
0
4
1
18
0
4
5
11
1
5
5
15
1
5
a.set(6, 7);
Error: 6 is larger than a.size();
5
2
2
3
5
2
2
3
remove(i)
removes the value at position from the ArrayList
0 <= i < size()
Size of the ArrayList is decreased by 1
a
1
18
0
4
5
15
1
5
a.remove(2);
a.remove(0);
a.remove(4);
3
2
2
3
1
5
18
15
0
1
3
4
5
2
0 15 1
3
Error: 4 is larger than a.size();
2
2
18
2
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
a.add(0, 1);
a.add(2, 3);
a.add(6, 12);
a.add(8);
a
a
5
2
0 15 1
3
1
5
18
15
0
1
3
4
1
18
0
4
1
0 15
5
Error: 8 is larger than a.size();
5 3
15
1
5
5
1
6
18
2
2
2
2
3
2
2
3
2
3
1812
4
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));
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:
827361
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
8
60
4i
1
60
4
1
60
4
2
7
11
2
5
Exchange
2
81
i 5
7
2
jMin
2
7
81
2
5 i jMin
Exchange
3
3
jMin
3
3
Dont exchange
3
3
1
60
4
2
3
81
2
i
5
Exchange
7
3
jMin
1
70
4
2
3
6
81
2
3
i jMin
5
Dont exchange
1
2
3
70
81
2
4
5
i = 4 = size();
done!
6
3