0

I have the array

String[] test_=new String[] {"a b c d", "f g h i","j k l s gf"};

Now i want to create another array that has the elements

{"b d", "g i","k s"}

how can I do this?

I've managed to separate the array into rows using

String split_test[] = null;
for (int j = 0 ; j <= 2 ; j++) {
      split_test=test_[j].split("\\s+");
      System.out.println(Arrays.toString(split_test));
}

But now I want to separate each of those rows, I tried the solution of How to Fill a 2d array with a 1d array? Combined with something like this split_test=test_[j].split("\s+"), but I haven't been able to solve it.

Also If I do what they say I have to make the array split_test have a number of specific columns, but what I want is the size of the columns of split_test depend of the array test_. For example in case I want to have an array with the elements {"b d", "g i", "k s gf"}

String[][] split_test = new String[3][2];
for(int row = 0; row < split_test.length; row++) {
    for(int col = 0; col < split_test[row].length; col++) {
        split_test[row][col] = test_[row];/*I still don't understand how to use the split within the for*/
        System.out.println(split_test[row][col]);
    }
}

Is there a simpler and more efficient way of doing this?

Thanks

Community
  • 1
  • 1
Tachekentya
  • 23
  • 1
  • 5

3 Answers3

0

You should use a 2 dimentional array, you can create one by doing:

String[][] input=new String[][] {{"a","b","c","d"}, {"f","g","h","i"},{"j","k","l","s"}};

You can then do something like this to retrieve {{"b","d"}, {"g","i"},{"k","s"}}:

String[][] output = new String[input.length][2];
for(int i = 0; i<input.length; i++)
{
  output[i] = new String[]{input[i][1],input[i][3]};
}

System.out.println(Arrays.deepToString(output));
Henrykvdb
  • 320
  • 1
  • 10
0

I have used a different approach that works as well. I noticed you only take the uneven indexes, hence my modulo approach:

    String[] array = new String[] {"a b c d", "f g h i","j k l s gf"};
    String[] result = new String[array.length];
    for(int i = 0; i < array.length; i++) {
        String subresult = "";
        String[] array2 = array[i].split(" ");
        for(int j = 0; j < array2.length; j++) {
            if(j % 2 == 1)
                subresult += array2[j] +" ";
        }
        result[i] = subresult.trim();
    }
Valentin Grégoire
  • 1,110
  • 2
  • 12
  • 29
0

Here is another one. You can use the substring method of String class. Or use indexes of the array returned by the split method.

    String output[] = new String[test_.length];
    String split_test[] = null;
    for (int j = 0; j < test_.length(); j++) {
        split_test = test_[j].split("\\s+");

        // use direct index
        // output2[j] = split_test[1] + " " + split_test[3];
        // or based on length
        output[j] = split_test[1] + " " + split_test[split_test.length - 2];
    }
    System.out.println(Arrays.toString(output));

Output:

b d
g i
k s
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41