-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicArrayListsOne.java
More file actions
36 lines (25 loc) · 1.08 KB
/
BasicArrayListsOne.java
File metadata and controls
36 lines (25 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
https://programmingbydoing.com/a/basic-arraylists-1.html
Create an ArrayList that can hold Integers. Add ten copies of the number -113 to the ArrayList. Then display the contents of the ArrayList on the screen.
This time, you must use a loop, both to fill up the ArrayList and to display it.
Also, in the condition of your loop, you should not count up to a literal number.
Instead you should use the size() of your ArrayList.
Notes: For loop will not work with size() because initial capacity is 10, but the SIZE is not 10
until it fills up.and someList.size(). Use a while loop.
*/
import java.util.*;
public class BasicArrayListsOne {
public static void main(String[] args) {
int numToAdd = -113;
int howManyToAdd = 10;
int index = 0;
ArrayList<Integer> someList = new ArrayList<Integer>(10);
while (someList.size() < howManyToAdd) {
someList.add(numToAdd);
}
while (index < someList.size()) {
System.out.println("Slot " + index + " contains a " + someList.get(index));
++index;
}
}
}