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 ?

1

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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
[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<fewPlanets.length;i++){System.out.println(fewPlanets[i]);}.
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 ;
}

Comments

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.

Comments

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

1 Comment

Arrays.copyOf is only Java 1.6 and later.
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.