0

What would be the best way to split a string array in certain index to make a string matrix removing the element you split. For example, for the string array ["Good","Bad","Sad"] if i split it at 1 it would give me a string matrix that looked like this [["Good"],["Sad"]]

2 Answers 2

5

You can use ArrayList instead of array. Removing a random element from an arraylist is quite easy since it is dynamic.

ArrayList>String> list = new ArrayList<String>();
...
list.remove(1);
Sign up to request clarification or add additional context in comments.

4 Comments

But LinkedList will be more efficient as we know.
And to seperate into different lists, there's a subList method for that. list.subList(0, 3) for example.
Did I misunderstand OP's question? Collecion api does make removing element easier than array. However (s)he wants a multi-dimension array as result. in fact, it would be 2D String array. And particularly with "multidimensional-array" tag. Did you suggest him/her first converting the array to list then converting the result list into array(2-d) again?
well what i need it for is for a tetris game and in order to use the collection framework i would have to rewrite a lot of code so i would really prefer to stick to arrays however if their is an easy way to convert an array to an arraylist and split it that way, that would be fine
1

well ivanovic's answer explains how to simply remove one element from a string sequence with java Collection (List). And it is indeed the straightforward way to achieve that goal (removing element).

However, my understanding of OP's question is, he gets an string array as parameter, and wants a 2-D String array to get returned. the "split-index" element should not be included in the result String[][].

Base on my understanding, I therefore add another answer:

final String[] input = new String[] { "one", "two", "three", "four", "five", "six", "seven", "eight" };
        final int len = input.length;
        final int pos = 3;
        final String[][] result = new String[2][Math.max(pos, len - pos - 1)];
        result[0] = Arrays.copyOf(input, pos);
        result[1] = Arrays.copyOfRange(input, pos + 1, len);

well this is even not a java-method, but it explains how to get the result. in the example above, the result would be a 2-d array, [[one, two, three], [five, six, seven, eight]]

EDIT:

wrap it in a method is easy:

public static String[][] splitStringArray(String[] input, int pos) {
        final int len = input.length;
        final String[][] result = new String[2][Math.max(pos, len - pos - 1)];
        result[0] = Arrays.copyOf(input, pos);
        result[1] = Arrays.copyOfRange(input, pos + 1, len);
        return result;
    }

Note that error handling part is not there, e.g. pos outofbound handling, NPE checking (input) etc. you could do it by yourself I believe.

2 Comments

Could you wrap this in a method maybe im not exactly sure what each variable is doing
@popgalop there was a hardcoded 3 in the code. it has been fixed. copy the method from my answer, try again.

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.