【问题标题】:Remove specific index from array in java从java中的数组中删除特定索引
【发布时间】:2014-11-10 16:48:46
【问题描述】:

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

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

【问题讨论】:

  • 你说的remove是什么意思?
  • 删除角色
  • 再次,删除字符是什么意思?
  • Here's the official tutorial on arrays.第一句话就有你的答案。
  • 使用数组列表及其 .remove() 方法。数组是不可变的,因此您不能“删除”一个,而是创建一个长度为 -1 的新数组,然后从源中填充它。

标签: java arrays character


【解决方案1】:

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

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

然后将旧数组替换为 remove() 的结果。

【讨论】:

  • 如果索引是最后一个元素,它会抛出 ArrayIndexOutOfBoundsException。因此,应该使用 continue 而不是 i++。
【解决方案2】:

如果你不想使用 ArrayList, arraycopy 是另一种选择:

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

i 是您要删除的索引。

希望我能帮上忙。

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

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

【讨论】:

  • 顺便说一句,我/你应该将它命名为 char[] wordchar[] letters。还有,你的定义是错误的,应该是:char[] word = {'c','d','f','h','j'};
【解决方案3】:

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

这里,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"
}  

小测试:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:
       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));   
          }
        }
      

      【讨论】:

        【解决方案6】:

        使用字符串类:

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

        在 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>
        

        【讨论】:

          猜你喜欢
          • 2020-06-25
          • 2021-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多