简体   繁体   中英

Inserting / deleting elements in array by user input

I'm a newbie at Java programming. I would like to ask how could I insert / delete the certain input from the user into the arraylist I generated. And it should display the new list that formed I already had a code.. but it's not working well.. Here's my code:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

class New1 {
    public static InputStreamReader r = new InputStreamReader (System.in);
    public static BufferedReader inp = new BufferedReader(r);

    public static void main (String args[]) throws Exception {
        ArrayList employees = new ArrayList();
        employees.add("A");
        employees.add("B");
        employees.add("C");
        employees.add("D");
        employees.add("E");

        Scanner scan1 = new Scanner (System.in);
        System.out.println ("Lists of Employees");
        System.out.println ("What do you want to do?:");
        System.out.println ("1 - Display list. \n2 - Insert New Name. \n3 - Delete an item. \n4 - Nothing." + "\n ");
        int task = scan1.nextInt();

        if (task==1) {
            System.out.println ("Contents of Employees:" + employees);
        } else if (task==2) {
            do {
                System.out.println("Current list is " + employees);
                System.out.println("Add more? (y/n) ");
                if (scan1.next().startsWith("y")) {
                    System.out.println("Enter : ");
                    employees.add(scan1.next());
                } else {
                    break;
                }
            } while (true);

            System.out.println("List is " + employees);
            String[] arr = employees.toArray(new String[0]);
            System.out.println("Array is " + Arrays.toString(arr));
        }
    }

I really need help here. >.<

First thing there are some compile time errors in your code. at last line of code, '}' is missing. class block is not closed.

Second, Use below line, String[] arr = (String[]) employees.toArray(new String[0]); instead of String[] arr = employees.toArray(new String[0]);

as toArray(new String[0]) will return an object and you are storing it in array. So , you have to typecast it into array.Now, it will work fine.

You should parameterized the list initialization otherwise the following piece of code won't compile: String[] arr = employees.toArray(new String[0]);

Although you can explicitly cast the right-hand expression to String[] , it's not a good practice and disregards the intent of generics to catch potential errors at compile time.

I would do this:

List<String> employees = new ArrayList<String>();

and instead of 0 , would use size of list to initialize size of the array being created:

String[] arr = employees.toArray(new String[employees.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM