简体   繁体   English

从数组错误中删除元素

[英]Removing element from array error

In the following code, if the size of the array is larger than 20, I'm trying to remove anything after 20 from the array. 在下面的代码中,如果数组的大小大于20,则尝试从数组中删除20之后的所有内容。 In my loop, I have userinput.remove(20 + i); 在我的循环中,我有userinput.remove(20 + i); However, I'm getting that it can't find the symbol remove? 但是,我发现它找不到符号删除? I'm not sure why it's doing this if the error.add itself is actually working. 如果error.add本身确实有效,我不确定为什么要这样做。

userinput is defined earlier in the code userinput在代码的前面定义

public static void checknames(String[] userinput){

ArrayList<String> error = new ArrayList<String> ();


    if(userinput.length > 20){

        for(int i=0; i<userinput.length - 20; i++){
            error.add(userinput[20 + i]);
            userinput.remove(20 + i);}
            JOptionPane.showMessageDialog(null, "You can only enter up to 20
            employees. \n The following employees exceed this limit." + error);

            }
        }

The error is correct - there is no such remove method for arrays. 错误是正确的-数组没有这种remove方法。 You should either: 您应该:

  • Use a List instead, like the ArrayList you have used for error . 改用List ,就像用于errorArrayList一样。
  • Create a new array which is 1 element shorter, and copy over everything except the element you are trying to remove. 创建一个新的数组,该数组要短1个元素,然后复制除要删除的元素之外的所有内容。

You cannot call remove an array. 您不能调用remove数组。 You can't change the size of the array. 您无法更改数组的大小。 But you could set that element to null : 但是您可以将该元素设置为null

userinput[20 + i] = null;
userinput.remove(20 + i);

userinput is array of String[] . userinputString[]数组。 There is not method remove(..) available for array. 没有可用于数组的方法remove(..)

May be you need to set value to null for indexes greater than 20 (or) create a new String array with only first 20 elements and discard userinput . 可能是您需要将索引大于20的值设置为null (或)创建仅具有前20个元素的新String数组,并丢弃userinput

Try this: 尝试这个:

public static void checknames(String[] userinput) {

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

        for(int i=20; i<userinput.length; i++) {
            error.add(userinput[i]);
            userinput[i] = null;
        }
        JOptionPane.showMessageDialog(null, "You can only enter up to 20
            employees. \n The following employees exceed this limit." + error);

}

Just a few little changes. 只是一些小变化。 You should always make ArrayList s like this(with List<...> ) on the left-hand side. 您应该始终在左侧使像这样的ArrayList (带有List<...> )。 Also, I got rid of the if statement and slightly changed your loop so you don't need it. 另外,我摆脱了if语句,并略微更改了您的循环,因此您不需要它。 And as everyone else mentioned, .remove(...) doesn't work with arrays. 正如其他所有人所提到的, .remove(...)不适用于数组。

If you insist on keeping the String[], you could delegate the "dirty work" to existing API methods, ie Arrays.copyOfRange(Object[] src, int from, int to) 如果您坚持保留String [],则可以将“脏工作”委托给现有的API方法,即Arrays.copyOfRange(Object [] src,int from,int to)


Short, Self Contained, Correct (Compilable), Example: 简短,自包含,正确(可编译),例如:

import java.util.Arrays;

public class R {
    public static String[] trimEmployees(String[] employees, int maxSize) {
        return Arrays.copyOfRange(employees, 0, maxSize);
    }

    public static void main(String[] args) {
        String[] employees = new String[] { "Jennifer", "Paul", "Tori",
                "Zulema", "Donald", "Aleshia", "Melisa", "Angelika", "Elda",
                "Elenor", "Kimber", "Eusebia", "Mike", "Karyn", "Marinda",
                "Titus", "Miki", "Alise", "Liane", "Suzanne", "Dorothy" };
        int max = 20;

        System.out.println(String.format("Input employees (len=%d): %s ",
                employees.length, Arrays.toString(employees)));
        if (employees.length > max) {
            employees = trimEmployees(employees, max);
            System.out.println(String.format("Trimmed employees (len=%d): %s",
                    employees.length, Arrays.toString(employees)));
        }
    }
}

Prints: 印刷品:

Input employees (len=21): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne, Dorothy] 
Trimmed employees (len=20): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne]

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

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