【问题标题】:Remove a specific string from an array of string从字符串数组中删除特定字符串
【发布时间】:2011-10-29 17:05:04
【问题描述】:

我有一个这样的数组:

String n[] = {"google","microsoft","apple"};

我想要做的是删除“苹果”。

我的问题很基本,但是,我搜索了网站,发现java并不真正支持从数组中删除功能。我也听说使用Java Utils,因为删除项目非常简单。 ...我试图在 google 上找到 Java Utils,但几乎所有链接都已失效。

那么最后...有没有办法从字符串数组中删除一个字符串?

即使我使用 ArrayList,我也找不到在其中生成随机项的方法!例如:在一个普通数组中,我生成一个这样的字符串:

String r = myAL[rgenerator.nextInt(myAL.length)];

在数组列表中它不起作用......也许你知道一个解决方案......

【问题讨论】:

  • 你可能被提到了包java.util,它包含一些下面的人用来回答你的问题的类,例如ListArrayList,它带有标准Java 库 - 无需单独下载。
  • 不!您想删除 Microsoft。

标签: java arrays string


【解决方案1】:

定义“删除”。

数组是固定长度的,一旦创建就不能调整大小。您可以将元素设置为null 以删除对象引用;

for (int i = 0; i < myStringArray.length(); i++)
{
    if (myStringArray[i].equals(stringToRemove))
    {
        myStringArray[i] = null;
        break;
    }
}

myStringArray[indexOfStringToRemove] = null;

如果您想要一个动态大小的数组,其中实际删除对象并相应调整列表(数组)大小,请使用ArrayList<String>

myArrayList.remove(stringToRemove); 

myArrayList.remove(indexOfStringToRemove);

编辑以回应 OP 对其问题的编辑并在下方发表评论

String r = myArrayList.get(rgenerator.nextInt(myArrayList.size()));

【讨论】:

  • 没关系,但我还需要从该列表中生成一个随机项目......在普通数组中很容易完成,但是在数组列表中很难......如果你知道如何请告诉我...
  • 为什么会“难做”? ArrayList 为您提供与数组相同的随机访问(通过索引)。请参阅上面的编辑。
  • 我推荐使用 (myStringArray[i].equalsIgnoreCase(stringToRemove))。这将确保无论用户输入的类型如何,大小写都不会阻止字符串从数组中删除。
【解决方案2】:

在一步中是不可能的,或者你需要保留对数组的引用。 如果您可以更改参考,这会有所帮助:

      String[] n = new String[]{"google","microsoft","apple"};
      final List<String> list =  new ArrayList<String>();
      Collections.addAll(list, n); 
      list.remove("apple");
      n = list.toArray(new String[list.size()]);

我不推荐以下方法,但如果您担心性能:

      String[] n = new String[]{"google","microsoft","apple"};
      final String[] n2 = new String[2]; 
      System.arraycopy(n, 0, n2, 0, n2.length);
      for (int i = 0, j = 0; i < n.length; i++)
      {
        if (!n[i].equals("apple"))
        {
          n2[j] = n[i];
          j++;
        }      
      }

我不推荐它,因为代码更难阅读和维护。

【讨论】:

  • 更不用说非常低效了;所有的复制都不是免费的。这就是发明 ArrayList 的原因。
【解决方案3】:

Java 中的数组不是动态的,就像集合类一样。如果你想要一个真正的支持动态添加和删除的集合,使用 ArrayList。如果你仍然想使用 vanilla 数组,找到字符串的索引,构造一个大小比原始数组小一的新数组,并使用System.arraycopy() 复制前后的元素。或者手动编写一个带有跳过的复制循环,在小数组上差异可以忽略不计。

【讨论】:

    【解决方案4】:

    您无法从数组中删除任何内容 - 它们始终是固定长度的。创建长度为 3 的数组后,该数组的长度始终为 3。

    您最好使用List&lt;String&gt;,例如ArrayList&lt;String&gt;:

    List<String> list = new ArrayList<String>();
    list.add("google");
    list.add("microsoft");
    list.add("apple");
    System.out.println(list.size()); // 3
    
    list.remove("apple");
    System.out.println(list.size()); // 2
    

    这样的集合通常比直接使用数组灵活得多。

    编辑:删除:

    void removeRandomElement(List<?> list, Random random)
    {
        int index = random.nextInt(list.size());
        list.remove(index);
    }
    

    【讨论】:

    • 没关系,但我还需要从该列表中生成一个随机项目......在普通数组中很容易完成,但是在数组列表中很难......如果你知道如何请告诉我...
    • @user1015311: 不,你的做法和数组一样——你只是根据索引而不是值来删除。 remove 的超载采用 int。只需在[0, list.size()] 范围内创建一个随机数,然后调用list.remove(index);
    【解决方案5】:
    import java.util.*;
    
    class Array {
        public static void main(String args[]) {
            ArrayList al = new ArrayList();
            al.add("google");
            al.add("microsoft");
            al.add("apple");
            System.out.println(al);
            //i only remove the apple//
            al.remove(2);
            System.out.println(al);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多