Answer to Question #289520 in Java | JSP | JSF for Kev

Question #289520

What's in There?

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:

  1. Create a variable that accepts a positive integer.
  2. Create an empty list. Then, using loops, add string values to your list using the input() function. The number of string values to be added are dependent on the inputted value on the first inputted integer.
  3. Create another variable that accepts a non-negative integer. The number must only range from 0 until the value of the first inputted integer.
  4. Using your understanding on accessing list elements, access and print out the list element having the index position of the second inputted integer.
1
Expert's answer
2022-01-25T10:46:57-0500
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));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog