简体   繁体   English

通过用户输入在数组中插入/删除元素

[英]Inserting / deleting elements in array by user input

I'm a newbie at Java programming. 我是Java编程的新手。 I would like to ask how could I insert / delete the certain input from the user into the arraylist I generated. 我想问一下如何将用户的某些输入插入/删除到生成的arraylist中。 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]); 其次,在下面的代码行中使用String [] arr =(String [])employee.toArray(new String [0]); instead of String[] arr = employees.toArray(new String[0]); 而不是String [] arr = employee.toArray(new String [0]);

as toArray(new String[0]) will return an object and you are storing it in array. 如toArray(new String [0])将返回一个对象,并将其存储在数组中。 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]); 您应该对列表初始化进行参数化,否则以下代码将无法编译: 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. 尽管您可以将右侧表达式显式地转换为String[] ,但这不是一个好习惯,并且无视泛型在编译时捕获潜在错误的意图。

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: 而不是0 ,将使用list的大小来初始化要创建的数组的大小:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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