by CodeChum Admin
Let's try defining the size of the list and create the contents of it on our own! And then, to give off a sense of excitement, let's try accessing the value of a list element in a random index position!
Let's do this fast!
Instructions:
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void input(List<String> list, int size, Scanner in) {
for (int i = 0; i < size; i++) {
System.out.print("Enter: ");
list.add(in.nextLine());
}
}
public static void main(String[] args) {
List<String> list = new LinkedList<>();
Scanner in = new Scanner(System.in);
System.out.print("Size: ");
int size = Integer.parseInt(in.nextLine());
input(list, size, in);
System.out.print("Index (0-" + (size - 1) + "): ");
int index = Integer.parseInt(in.nextLine());
System.out.println(list.get(index));
}
}
Comments
Leave a comment