Home > Blockchain >  Java linked list using user input
Java linked list using user input

Time:10-01

I'm only a few weeks into a java class and I'm unsure how to proceed. Would anyone be able to direct me to the starting points of creating a linkedList using user input? Here's what I have so far... just the request for user input... what I'm hoping to discover is how I would take the results and create my linkedlist? Would I create the list within the first brackets or do I create a new bracket for this direction?

import java.util.LinkedList;
import java.util.Scanner;
import java.util.*;

public class Main {
    

  public static void main (String[]args) {

    Scanner scan = new Scanner (System.in);

      System.out.println ("Enter first name of equipment, cost and gain:");
    String equipment1 = scan.nextLine ();

      System.out.println ("Enter second name of equipment, cost and gain:");
    String equipment2 = scan.nextLine ();

      System.out.println ("Enter third name of equipment, cost and gain:");
    String equipment3 = scan.nextLine ();

      System.out.println ("You have entered:");
      System.out.println (equipment1);
      System.out.println (equipment2);
      System.out.println (equipment3);

CodePudding user response:

List is an interface, LinkedList implements List.

You can do as follows:

import java.util.List;

List<String> list = new LinkedList<>();
list.add(equipment1)
list.add(equipment2)
list.add(equipment3)

In addition, you can do also:

import java.util.List;

List<String> list = new LinkedList<>(Arrays.asList(equipment1, equipment2, equipment3));

Which initialize the LinkedList and insert in the constructor.

  • Related