java 从java中的数组中删除特定索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26848762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:45:18  来源:igfitidea点击:

Remove specific index from array in java

javaarrayscharacter

提问by GES Vadamanappakkam

Can I remove a specific element from array by mentioning index value? For example can I remove the character dby giving indexvalue 1?

我可以通过提及索引值从数组中删除特定元素吗?例如,我可以d通过赋予index值 1来删除字符吗?

char[] words = { 'c', 'd', 'f', 'h', 'j' };

回答by lenhuy2106

If you don't want to use ArrayList, arraycopyis an alternative:

如果您不想使用 ArrayList,则 arraycopy是一种替代方法:

    System.arraycopy(words, 0, result, 0, i);
    System.arraycopy(words, i+1, result, i, result.length-i);

where iis your index to delete.

其中i是您要删除的索引。

Hope I can help.

希望我能帮上忙。

EDIT: Of course you should initially define the correct array lengths:

编辑:当然,您最初应该定义正确的数组长度:

char[] result = new char[words.length-1];

回答by Fjotten

Assuming you do not want your array to contain null values, then you would have to make a method that does it. Something like this should suffice:

假设您不希望数组包含空值,那么您必须创建一个方法来执行此操作。像这样的东西应该足够了:

public char[] remove(int index, char[] arr) {
    char[] newArr = new char[arr.length - 1];
    if(index < 0 || index > arr.length) {
        return arr;
    }
    int j = 0;
    for(int i = 0; i < arr.length; i++) {
        if(i == index) {
            i++;
        }
        newArr[j++] = arr[i];
    }

    return newArr;
}

Then just replace the old array with the result of remove().

然后只需用remove() 的结果替换旧数组。

回答by Alex Salauyou

If you need to remove one or multiple elements from array without converting it to Listnor creating additional array, you may do it in O(n) not dependent on count of items to remove.

如果您需要从数组中删除一个或多个元素而不将其转换为List或创建额外的数组,您可以在 O(n) 中执行此操作,而不依赖于要删除的项目数。

Here, ais initial array, int... rare distinct ordered indices (positions) of elements to remove:

这里,a是初始数组,int... r是要删除的元素的不同有序索引(位置):

public int removeItems(Object[] a, int... r) {
    int shift = 0;                             
    for (int i = 0; i < a.length; i++) {       
        if (shift < r.length && i == r[shift])  // i-th item needs to be removed
            shift++;                            // increment `shift`
        else 
            a[i - shift] = a[i];                // move i-th item `shift` positions left
    }
    for (int i = a.length - shift; i < a.length; i++)
        a[i] = null;                            // replace remaining items by nulls

    return a.length - shift;                    // return new "length"
}  

Small testing:

小测试:

Character[] words = {'c','d','f','h','j'};
removeItems(words, 1);
System.out.println(Arrays.asList(words));       // [c, f, h, j, null]

回答by Messi

 import java.util.Arrays;
  public class Main
 {
 public static void main(String[] args) {
 int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};

 int removeIndex = 1;
 int j=0;
  for(int i = 0; i < my_array.length -1; i++)
  {

    if(i==1)
    {

    }
    else
     {
        my_array[j] = my_array[i];
        j++;
     }

    }
     System.out.println("Original Array : "+Arrays.toString(my_array));   
    }
  }

回答by smac89

Using String class:

使用字符串类:

char[] words = { 'c', 'd', 'f', 'h', 'j' };
String str = new String(words);
words = (str.substring(0, Math.min(1, words.length)) + str.substring(Math.min(1 + 1, words.length))).toCharArray();

Running in jshell:

在 jshell 中运行:

jshell> char[] words = { 'c', 'd', 'f', 'h', 'j' };
words ==> char[5] { 'c', 'd', 'f', 'h', 'j' }

jshell> String str = new String(words);
str ==> "cdfhj"

jshell> words = (str.substring(0, Math.min(1, words.length)) + str.substring(Math.min(1 + 1, words.length))).toCharArray();
words ==> char[4] { 'c', 'f', 'h', 'j' }

jshell>

回答by Opalium

You can't remove an element from the array and "reduce" the array size. Once you have created an array, it's length is fixed.

您不能从数组中删除元素并“减小”数组大小。一旦你创建了一个数组,它的长度就是固定的。

You could change the value to something that has no meaning or is considered "empty", but you can't remove it.
Another option is to use a list, such as an ArrayList. It has a "remove" method that allows you to actually remove the element from it.

您可以将值更改为没有意义或被视为“空”的值,但无法删除它。
另一种选择是使用列表,例如 ArrayList。它有一个“remove”方法,允许您从其中实际删除元素。