0

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. In my loop, I have 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.

userinput is defined earlier in the code

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);

            }
        }
4

5 回答 5

3

错误是正确的——数组没有这样remove的方法。您应该:

  • 改用 a List,就像ArrayList您使用的error.
  • 创建一个短 1 个元素的新数组,并复制除您要删除的元素之外的所有内容。
于 2013-04-22T21:01:54.870 回答
0

您不能调用remove数组。您无法更改数组的大小。但是您可以将该元素设置为null

userinput[20 + i] = null;
于 2013-04-22T21:01:37.363 回答
0
userinput.remove(20 + i);

userinput是 的数组String[]。没有remove(..)可用于数组的方法。

可能您需要null为大于 20 的索引设置值(或)创建一个String只有前 20 个元素的新数组并丢弃userinput

于 2013-04-22T21:01:47.640 回答
0

尝试这个:

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);

}

只是一些小改动。您应该始终在左侧制作ArrayList这样的 s(带)。List<...>另外,我删除了该if语句并稍微更改了您的循环,因此您不需要它。正如其他人提到的那样,.remove(...)它不适用于数组。

于 2013-04-22T21:04:51.367 回答
0

如果您坚持保留 String[],您可以将“肮脏的工作”委托给现有的 API 方法,即Arrays.copyOfRange(Object[] src, int from, int to)


简短、独立、正确(可编译),示例:

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)));
        }
    }
}

印刷:

输入员工 (len=21): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne, Dorothy ]
精简员工 (len=20):[Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne]
于 2013-04-22T21:26:05.117 回答