advice Question

Search for string in 2 dimensional string array

Avatar of Tore
ToreFlag for Norway asked on
Java
13 Comments1 Solution25 ViewsLast Modified:

This relates to a previous question ( .. i forgot to get an answer before i closed it :( ) 


How do i search for a string in a 2 dim string array (column 0)

I have the following 2 dim string table. It is sorted by its first column.

I then thought i could use a Array.binarySearch (works fine for a one dimensional array) but i can't get my head around on how to do it on a 2 dim.

I need some expert advice.


import java.util.Arrays;
import java.util.Comparator;

public class TwoDimStringTable {
    
    public static void main(String [] args) {
        int i = 0;
        
        String[][] sTabell = new String[8][2];
        sTabell[0][0] =  "Ola";                sTabell[0][1] =  "Ola";
        sTabell[1][0] =  "var";                sTabell[1][1] =  "kunne";
        sTabell[2][0] =  "fra";                sTabell[2][1] =  "engelsk";
        sTabell[3][0]=   "Sandefjord";         sTabell[3][1]=   "ei";
        sTabell[4][0] =  "Han";                sTabell[4][1] =  "men";
        sTabell[5][0] =  "var";                sTabell[5][1] =  "han";
        sTabell[6][0] =  "lettmatros";         sTabell[6][1] =  "klarte";
        sTabell[7][0] =  "ombord!";            sTabell[7][1] =  "seg";

        for ( i = 0; i <= sTabell.length-1; i++) {
            System.out.println(sTabell[i][0] + "  " + sTabell[i][1]);
        }
        
        System.out.println(Arrays.deepToString(sTabell));
        for (i = 0; i < sTabell.length; i++) {
            Arrays.sort(sTabell[i]);
        }
        System.out.println(Arrays.deepToString(sTabell));
        Arrays.sort(sTabell, new Comparator<String[]>() {
            
            public int compare(String[] r1, String[] r2) {
                return Arrays.compare(r1, r2);
            }
        });
        System.out.println(Arrays.deepToString(sTabell));
        
        for ( i = 0; i <= sTabell.length-1; i++) {
            System.out.println("i = " + i + " " + sTabell[i][0] + "  " + sTabell[i][1]);

         // Question: how to get index of string "Sandefjord"

        }
      }
   }

Open in new window

ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 13 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 13 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004