1

I have a 2D String Array in java where i is rows and j is cols.

I'm looking for an elegant solution to remove cols in the following array.

String[i][j] data; 

The columns provided can either be a single column or more than one column i.e. if the columns to be removed are (7,8) then all values on data[i][7] and data [i][8] should be removed from the array.

I tried various solutions such as declaring a new array and copying from the previous array excluding the columns but I'm wondering if there's a more elegant solution I can use. Any leads?

3
  • In general if you're going to be modifying the size, don't use an array - use an ArrayList or some other more flexible type. (In fact, 90% of the time I'd just use one of those to begin with. Arrays are a pain.) Commented Nov 21, 2022 at 21:57
  • Yessir, I agree. But in this case, I have the data coming from somewhere else and unfortunately, I can't do anything about it at the moment. :( @EdwardPeters Commented Nov 21, 2022 at 21:58
  • 1
    Do you have to keep the data as an array? If it comes in as one but the future of the data is up to you after that point, I'd generally say it's worth converting. If you have to output the modified data in the same format, it might be worth converting anyway, and converting back later. If you want a more elegant solution that's probably it. (It might be slower if these are large arrays, but that's probably not something you need to be concerned with.) If ArrayList just didn't exist, yeah, probably just make a new array and copy. Commented Nov 21, 2022 at 22:01

1 Answer 1

1

I took pity on you and wrote this. I think this is what "elegant" looks like in Java?

import java.util.*;
import java.util.stream.*;

class ArrayMangler {
    public static void main(String[] args) {
        String[] sample1 = {"Adam", "Bob", "Charlie", "Donald"};
        String[] sample2 = {"Ernst", "Ferdinand", "Godfrey", "Hrothgar"};
        String[] sample3 = {"Iago", "Jormangandr", "Killroy", "Lorelei"};
        String[][] input = {sample1, sample2, sample3};
        
        List<Integer> badIndexes = Arrays.asList(1, 3);
        String[][] output = removeColumns(input, badIndexes);
        for (String[] row : output){
            System.out.println(Arrays.toString(row));
        }
        
    }
    public static String[][] removeColumns(String[][] input, List<Integer> indexesToTrim){
        return Arrays.stream(input)
            .map(arr -> removeFromRow(arr, indexesToTrim))
            .toArray(String[][]::new);
    }
    public static String[] removeFromRow(String[] input, List<Integer> indexesToTrim){
        return IntStream
            .range(0,input.length)
            .filter(i ->!indexesToTrim.contains(i))
            .mapToObj(i -> input[i])
            .toArray(String[]::new);
    }
}
Sign up to request clarification or add additional context in comments.

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.