int r [] = new int[4]
r[0] = 88;
r[1] = 45;
r[2] = 34;
r[3] = 47;
The following is the process of generating a random number less than 10 and storing it in the ArrayList until 0 is output.
import java.util.ArrayList; //Import.
import java.util.Random;
public class PracticeList {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>(); //Generate
Random random = new Random();
while(true){
int randomValue = random.nextInt(10);
if(randomValue == 0){
break;
}
al.add(randomValue); //Store one by one until 0 appears
}
for (Integer n: al) { //Turn with a special for statement and display one by one
System.out.print(n); //Output result example: 234245...
}
As a method, mainly
add ()-> Add element (used in the above example)
size ()-> Get number of elements
remove ()-> remove the element with the specified number
isEmpty ()-> returns true when there are no elements in the list
Recommended Posts