1

I have a list of words , there are 4 words, it cant contain more that 4 its just an example. I want to use just 2 of the words the rest of them should be ignored or deleted e.g :

    String planets = "Moon,Sun,Jupiter,Mars";
    String[] planetsArray = planets.split(",");
    int numberOfPlanets = planetsArray.length;

the result i get is 4. How do i delete the rest of the words if my list contains more that 2 words ?

Kiesa
  • 407
  • 1
  • 19
  • 42
  • 1
    Have you searched for any similar questions here? This search brings up several questions you would be interested in. http://stackoverflow.com/search?q=delete+from+array+java – Kevin D Oct 13 '11 at 09:26

5 Answers5

1
String planets = "Moon,Sun,Jupiter,Mars";
String[] planetsArray = planets.split(",");
if(planetsArray .length > 2){
  String []newArr = new String[2];
  newArr[0]=planetsArray [0];
  newArr[1]=planetsArray [2];
  planetsArray = newArr ;
}
jmj
  • 237,923
  • 42
  • 401
  • 438
1

Use Arrays.asList to get a List of Strings from String[] planetsArray.

Then use the methods of the List interface -contains,remove,add, ...- to simply do whatever you want on that List.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

If you need to select the first 2 planets just copy the array:

String[] newPlanetsArray = Arrays.CopyOf(planetsArray, 2);

If you need to select 2 specific planets you can apply the following algorithm:

First, create a new array with 2 elements. Then, iterate through the elements in the original array and if the current element is a match add it to the new array (keep track of the current position in the new array to add the next element).

String[] newPlanetsArray = new String[2];

for(int i = 0, int j = 0; i < planetsArray.length; i++) {
   if (planetsArray[i].equals("Jupiter") || planetsArray[i].equals("Mars")) {
      newPlanetsArray[j++] = planetsArray[i];
      if (j > 1)
         break;
   }
}
bruno conde
  • 47,767
  • 15
  • 98
  • 117
1

As suggested in your previous question, you can use

String[] fewPlanets = new String[]{planets[0], planets[1]};

Just make sure the planets array has 2 elements or more to avoid an ArrayIndexOutOfBoundsException. You can use length to check it: if (planets.length >= 2)

For a more sophisticated solution, you could also do this using System.arrayCopy() if you're using Java 1.5 or earlier,

int numberOfElements = 2;
String[] fewPlanets = new String[2];
System.arraycopy(planets, 0, fewPlanets, 0, numberOfElements);

or Arrays.copyOf() if you're using Java 1.6 or later:

int numberOfElements = 2;
String[] fewPlanets = Arrays.copyOf(planets, numberOfElements);
Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • String planets = "Moon,Sun,Jupiter,Mars"; String[] planetsArray = planets.split(","); String[] fewPlanets = new String[]{planetsArray[0], planetsArray[1]}; System.out.println(fewPlanets); i tryed this but i get some weird stuff if i print it out : [Ljava.lang.String;@67af7a – Kiesa Oct 13 '11 at 09:58
  • `[Ljava.lang.String;@67af7a` is what `fewPlanets.toString()` is returning. That is an un unique identifier for the instance. If you want to print the contents of the array, iterate over its elements and print each of them. Something like: `for (int i=0;i – Xavi López Oct 13 '11 at 10:13
0

You could use an idea from How to find nth occurrence of character in a string? and avoid reading the remaining values from your comma separated string input. Simply locate the second comma and substring upto there

(Of course if your code snippet is just an example and you do not have a comma separated input, then please ignore this suggestion :)

Community
  • 1
  • 1
aldrin
  • 4,482
  • 1
  • 33
  • 50