0
private String[] names = { "bobby", "jones", "james", "george", "cletus", "don", "joey" };

public String getName() {
    Random random = new Random();
    String name = "";
    int num = random.nextInt(names.length-1);
    name = names[num];
    names[num] = null; //HOW TO REMOVE FROM THE LIST???
    return name;
}

I cannot remember how to remove the item from the list, please help.

This was my solution, thank everyone very much!

private String[] names = { "bobby", "jones", "james", "george", "cletus", "don", "joey" };
ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));

public String getName() {
    Random random = new Random();
    String name = "";
    int num = random.nextInt(names.length - 1);
    name = list.get(num);
    list.remove(num);
    return name;
}
  • 3
    You don't remember because you can't remove elements from an array; however you can create a new array and copy only the elements you want (Hint: use `System.arrayCopy()`) – morgano Nov 03 '15 at 05:49
  • check this out http://stackoverflow.com/questions/112503/how-do-i-remove-objects-from-an-array-in-java – Rockstar Nov 03 '15 at 05:50

2 Answers2

1

Array is a fixed size datastructure. You cannot reduce the size of it. You can however, overwrite contents and maintain a counter which tells you the effective size. Basically, you shift the entries to the left by one slot.

In your example, you can do something like this:

Let us assume that you want to remove a[5] and there are 10 elements in the array.

for( int inx = 5; inx < 9; inx++ )
{
    array[inx] = array[inx+1]
}
int arrayLength = array.length; // Because you are overwriting one entry.

With this, your array would now look like

Before this code:

"bobby", "jones", "james", "george", "cletus", "don", "joey", "pavan", "kumar", "luke"

After this code:

"bobby", "jones", "james", "george", "cletus", "joey", "pavan", "kumar", "luke", "luke"

We have overwritten the "don" entry here. And we now have to maintain a new counter which is now going to be the length of the array and this is going to be one less than array.length. And you would be using this new variable for processing the array.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
0

In the program you are using string array. If you want to remove element from list then you can remove element in this way :

for (Iterator<String> iter = list.listIterator(); iter.hasNext(); ) {
  String a = iter.next();
  if (//remove condition) {
     iter.remove();
  }
}

If you want remove all elemnt from list then you can use this line :

list.removeAll(//Your list);

You can do in this way :

String[] names = { "bobby", "jones", "james", "george", "cletus", "don", "joey" };
List<String> list = new ArrayList<String>(Arrays.asList(names));
Random random = new Random();   
int num = random.nextInt(names.length-1);    
list.remove(num);    
System.out.println(Arrays.asList(list.toArray(new String[list.size()])));  //Print the value of updated list
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • I got this error in the for statement, for "list.listIterator()": Cannot invoke listIterator() on the array type String[] BTW please keep in mind I have never worked with iterator's before – IshThaFish Nov 03 '15 at 05:56
  • Look at my updated post, that is what I used and it works perfectly. Thanks soo much I would +rep your post but I'm too low of a rank. – IshThaFish Nov 03 '15 at 06:09