Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to get the array that text belongs in

I have a multidimensional array as follows:

[[Acid Exposure (pH),  Upright], [Recumbent, Total], [,  Upright Normal Recumbent Normal Total], [Normal],]
[[Bolus Exposure (Impedance)Upright], [,  Recumbent, Total], [,  Upright Normal Recumbent Normal Total], [Normal]]
[[Postprandial Data], [,  Postprandial Interval:  120 min]]

I want to create a new Array if the inner array contains the line "Acid Exposure (pH)" so that I end up with just [Acid Exposure (pH), Upright], [Recumbent, Total], [, Upright Normal Recumbent Normal Total], [Normal],] in a new array.

I tried this:

 ArrayList<String> matches = new ArrayList<String>();
 Pattern p = Pattern.compile("Acid Exposure \\(pH\\)");

     for (List<String> row:twoDim) {
         for (String cell:row){
            if (p.matcher(cell).matches()) {
              matches.add(cell);
             System.out.println(matches);
            }
     }
 }

but it just gave me [Acid Exposure (pH)]

Upvotes: 0

Views: 58

Answers (4)

Svetlana
Svetlana

Reputation: 26

int find = 0;

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

    for(int i=0; i < initialArray.size();i++)
    {

        String[][] row = (String[][]) initialArray.get(i);

        for(int k = 0; k < row.length; k++ )
        {
            for(int l=0; l < row[0].length; l++)
            {
                if (row[k][l].equals("Acid Exposure (pH)"))
                {

                    find = i;
                    for(int p =0; p < row[k].length; p++)
                    {
                        newarray.add(row[k][p]);
                    }
                    break;


                }
            }
        }           
    }

Upvotes: 0

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

You are just checking if you had a match in the List and only add this one specific item. You rather want to set a flag and add the whole List after you found a match. Untested, but it should look like this:

// Just a hint, define matches a List to be able to change the actuall class of the List more easy.
List<String> matches = new ArrayList<String>();
Pattern p = Pattern.compile("Acid Exposure \\(pH\\)");
for (List<String> row : twoDim) {
    boolean found = false;
    for (String cell : row) {
        if (p.matcher(cell).matches()) {
            System.out.println(matches);
            found = true;
            // Break out of the loop, no need to check for matches anymore
            break;
        }
    }
    if (found) {
        matches.addAll(row);
    }
}

Upvotes: 1

Maciek
Maciek

Reputation: 112

You wanted to add an entire row but added only cell.

Change

matches.add(cell);

to

matches.addAll(row)

Upvotes: 0

java_maniac
java_maniac

Reputation: 101

You are just printing out the matched cell in the code and what you need is the entire row.

Why don't you modify the type of 'matches' and add the row you want to the matches.

Upvotes: 0

Related Questions