Remove a specific string from an array of string

前端 未结 5 1556
梦谈多话
梦谈多话 2020-12-03 13:45

I have an array like this:

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

What I want to do is to remove \"apple\".

My problem

相关标签:
5条回答
  • 2020-12-03 14:27

    You can't remove anything from an array - they're always fixed length. Once you've created an array of length 3, that array will always have length 3.

    You'd be better off with a List<String>, e.g. an ArrayList<String>:

    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
    

    Collections like this are generally much more flexible than working with arrays directly.

    EDIT: For removal:

    void removeRandomElement(List<?> list, Random random)
    {
        int index = random.nextInt(list.size());
        list.remove(index);
    }
    
    0 讨论(0)
  • 2020-12-03 14:33

    Define "remove".

    Arrays are fixed length and can not be resized once created. You can set an element to null to remove an object reference;

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

    or

    myStringArray[indexOfStringToRemove] = null;
    

    If you want a dynamically sized array where the object is actually removed and the list (array) size is adjusted accordingly, use an ArrayList<String>

    myArrayList.remove(stringToRemove); 
    

    or

    myArrayList.remove(indexOfStringToRemove);
    

    Edit in response to OP's edit to his question and comment below

    String r = myArrayList.get(rgenerator.nextInt(myArrayList.size()));
    
    0 讨论(0)
  • 2020-12-03 14:37

    It is not possible in on step or you need to keep the reference to the array. If you can change the reference this can help:

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

    I not recommend the following but if you worry about performance:

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

    I not recommend it because the code is a lot more difficult to read and maintain.

    0 讨论(0)
  • 2020-12-03 14:40
    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 14:44

    Arrays in Java aren't dynamic, like collection classes. If you want a true collection that supports dynamic addition and deletion, use ArrayList<>. If you still want to live with vanilla arrays, find the index of string, construct a new array with size one less than the original, and use System.arraycopy() to copy the elements before and after. Or write a copy loop with skip by hand, on small arrays the difference will be negligible.

    0 讨论(0)
提交回复
热议问题