简体   繁体   English

如何从 Java 中的数组中删除元素

[英]How can i remove an element from array in Java

Hello everyone i am trying to remove an name that the user has put in from an String Array, i am new to programming and i have tried this but it doesn't work.大家好,我正在尝试删除用户从字符串数组中输入的名称,我是编程新手,我已经尝试过这个,但它不起作用。 Can someone help me or tell me what i am doing wrong?有人可以帮助我或告诉我我做错了什么吗?

    String [] myName = {"Testname","Charel","melissa","Kelly"};

   removeName(myName);

    public void removeName(String[] names )
    {
        Scanner sc =  new Scanner(System.in);
        String name = "";
         name = sc.nextLine();
        for (int i = 0; i < names.length; i++) {
           name = names[i-1];
        }
    }

How can i do this?我怎样才能做到这一点?

You probably need to use Lists for this.您可能需要为此使用列表 Your list will be a list of String, and use remove() method to do this.您的列表将是一个字符串列表,并使用 remove() 方法来执行此操作。

An array's length is fixed and can't be changed this way.数组的长度是固定的,不能以这种方式更改。

Useful Link : Removing items from a list有用的链接: 从列表中删除项目

First off, an array does not change size after it is initialized, the only way to change the size of an array is to replace it with a new array!首先,数组在初始化后不会改变大小,改变数组大小的唯一方法是用新数组替换它! So in order to not end up with a double entry or an empty field, you would need to make a new array that is one size shorter, and write the names you want to keep into that.因此,为了不以双重条目或空字段结束,您需要制作一个短一号的新数组,并将您想要保留的名称写入其中。

An array might be ill-suited for your purposes, so consider using a list or an ArrayList.数组可能不适合您的目的,因此请考虑使用 列表或 ArrayList。 A list can be resized, so removing an element will automatically shorten the list.列表可以调整大小,因此删除元素会自动缩短列表。 I recommend you look into that.我建议你调查一下。

Lastly, you currently aren't even comparing your input to your fields.最后,您目前甚至没有将您的输入与您的字段进行比较。 Replace name = names[i-1];替换name = names[i-1]; with something along the lines of有类似的东西

if(name.equals(names[i]))
//TODO: Remove from list

See here for more details about String.equals()!有关 String.equals() 的更多详细信息,请参见此处

Also, keep in mind that the user input might not match any name at all, so prepare for that case as well!另外,请记住,用户输入可能根本不匹配任何名称,因此也要为这种情况做好准备!

To remove an element from an array in Java, you need to create a new array and copy over all the elements you want to keep.要在 Java 中从数组中删除元素,您需要创建一个新数组并复制所有要保留的元素。 That is because Java arrays are fixed-size.那是因为 Java 数组是固定大小的。

For example, to remove an element at a particular index, you could do it like this:例如,要删除特定索引处的元素,您可以这样做:

public static String[] remove(String[] array, int index) {
    String[] result = new String[array.length - 1];
    System.arraycopy(array, 0, result, 0, index);
    System.arraycopy(array, index + 1, result, index, result.length - index);
    return result;
}

You would then remove melissa from your array as follows:然后,您将从您的阵列中删除melissa ,如下所示:

String[] names = { "Testname", "Charel", "Melissa", "Kelly" };
names = remove(names, 2);
System.out.println(Arrays.toString(names));

Output输出

[Testname, Charel, Kelly]

Of course, it would be much easier to do it using a List :当然,使用List会容易得多:

List<String> names = new ArrayList<>(Arrays.asList("Testname", "Charel", "Melissa", "Kelly"));
names.remove(2);
System.out.println(names);

Or:或者:

List<String> names = new ArrayList<>(Arrays.asList("Testname", "Charel", "Melissa", "Kelly"));
names.remove("Melissa");
System.out.println(names);

Output of both is the same as above.两者的输出与上面相同。

Hello everyone i am trying to remove an name that the user has put in from an String Array, i am new to programming and i have tried this but it doesn't work.大家好,我正在尝试从字符串数组中删除用户输入的名称,我是编程新手,但是我已经尝试过了,但是它不起作用。 Can someone help me or tell me what i am doing wrong?有人可以帮我还是告诉我我做错了什么?

    String [] myName = {"Testname","Charel","melissa","Kelly"};

   removeName(myName);

    public void removeName(String[] names )
    {
        Scanner sc =  new Scanner(System.in);
        String name = "";
         name = sc.nextLine();
        for (int i = 0; i < names.length; i++) {
           name = names[i-1];
        }
    }

How can i do this?我怎样才能做到这一点?

There are some simple methods using java api provide by jdk, for example: jdk提供了一些使用java api的简单方法,例如:

    String [] myName = {"Testname","Charel","melissa","Kelly"};
    List<String> container = new ArrayList(Arrays.asList(myName));
    container.remove("Charel");
    String[] result = new String[myName.length - 1];
    container.toArray(result);

Alternatively you can also use this to convert array to list,或者,您也可以使用它来将数组转换为列表,

    Collections.addAll(container, myName);
String [] myName = {"Testname","Charel","melissa","Kelly"};

removeName(myName);

public void removeName(String[] names )
{
    Scanner sc =  new Scanner(System.in);
    String name = sc.nextLine();

    for (int i = 0; i < names.length; i++) {
        if(names[i]==name)
        {
            for(int j=i;j<names.length-1;j++)
            {
                names[j]=names[j+1];
            }
        }
    }
}

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

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