cours_progra/bac1/q2/algo/tp8/ArrayListTest.java

55 lines
1.0 KiB
Java
Raw Normal View History

2023-05-05 13:02:58 +02:00
import java.util.ArrayList;
import java.util.Random;
public class ArrayListTest {
public static ArrayList<String> create(int n){
ArrayList<String> ret = new ArrayList<String>();
for (;n > 0 ; n--) {
String random_Word = "";
Random rand = new Random();
int str_size = rand.nextInt(1, 50);
for(int i =0; i < str_size; i++){
random_Word += (char) rand.nextInt(32, 126);
}
ret.add(random_Word);
}
return ret;
}
public static void add(ArrayList<String> l, String s){
l.add(s);
}
public static void empty(ArrayList<String> l){
l.clear();
}
public static void display(ArrayList<String> l){
System.out.print("[");
for (String e : l) {
System.out.print(e);
System.out.print(", ");
}
System.out.println("]");
}
public static void main(String[] args) {
ArrayList<String> a;
a = create(5);
display(a);
add(a, "The lazy brown fox jump over the lazy fence");
display(a);
empty(a);
display(a);
add(a, "The lazy brown fox jump over the lazy fence");
display(a);
}
}