0

I have an ArrrayList<String[][]> with a couple of String[][] in it. I want to extract out the 2D array that have same name as my String value.

I have tried the method below and it end up with errors like:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Code in my fragment:

//String value

String value = getArguments().getString("choices");

//2D arrays

String[][] volume = {
        {"(pascal)", "1"},
        {"(kilopascal)", "0.001"},
        {"(bar)", "0.00001"}}
String[][] length = {
        {"(Meter)", "1"},
        {"(Centimeter)", "100"},
        {"(Kilometer)", "0.001"}}
String[][] weight = {
        {"(kilometer per hour)", "1"},
        {"(mile per hour)", "0.6213711922"},
        {"(yard per hour)", "1093.6132983333"}}

//Array List

ArrayList<String[][]> stringList = new ArrayList<>();
stringList.add(weight);
stringList.add(length);
stringList.add(volume);

//Code for extracting the String[][] from arraylist

    ArrayList<String> data = new ArrayList<>();

    try {
        for (int i = 0; i < stringList.size(); i++) {
            if(stringList.get(i).toString().equals(value)){
                for (int y = 0; y < stringList.get(i).length;) {
                    data.add(stringList.get(i)[y][0]);
                    y++;
                }
            }
        }
    }catch (Exception e){
        Log.e(TAG, "onViewCreated: " + e );
    }

2 Answers 2

1

This really seems to be a scenario where you could use HashMap<String, Double>. It keeps the values in this form: key (of type String) -> value (of type Double). So you could completely remove your whole String[][] complication.

Here is an example:

String value = getArguments().getString("choices");

//HashMaps

HashMap<String, Double> map = new HashMap<>();
map.put("(pascal)", 1d);
map.put("(kilopascal)", 0.001);
// and so on

// You should add everything here though and remove the need for an ArrayList. that just complicates things

map.get(value); // Returns the double value

I cannot understand exactly what you want to do and what is supposed to be in the value variable. When you need to differentiate the map types (volume, length etc..) add yet another map that as key has String and as value another HashMap<String, Double>.

P.S: Try to log everything when you don't understand what is happening, some of your toString() functions, return the reference of the object and not what you (most likely) expect.

Or even better, use a debugger.

Sign up to request clarification or add additional context in comments.

Comments

0

By doing stringList.get(i).toString() you get the String[][] object cast toString(). That only returns something like this [[Ljava.lang.String;@762efe5d.

So comparing this to the value is wrong. Look up what stringList.get(i)[0][0] and stringList.get(i)[0][1] is, to figure out yourself what to compare and what you need.

By what I'm given i don't know which value exactly you are looking for, but it should be pretty easy to figure that out yourself.

You can name y as row or column to make it more readable.

Also instead of String[][] you can maybe use a HashMap<String, String> to make things easier.

2 Comments

Thanks for the answer , but I'm not looking to compare my String data to value in my String [ ][ ] , I just want to check if string data was (weight) it returns me the String[ ][ ] with name of weight so i can put it in another loop to extract the [i][0] from it.
@Daniel One technique i could think of is: Make the List a HashMap<String, String[][]> stringList, and then stringList.add("weight", weight); etc... This way you can choose the right string array based on its name. I do not think that there is an easy way to get the name of a variable. And this way you can omit the first for loop and just say String[][] chosen = stringList.get(value)

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.