2

The idea is to realize the possibility to store some string variables that are relating to each other. So that I can find one (or two) of the element using the other one (like in the code below). Database cannot be used. Code:

    public static void main(String[] args) {

    String[][] codesArrS = {{" ", "t", "n"}, {"231", "12223", "98745"}, {"Ver", "Gutva2", "Brezstel"}};
    String var0 = "t";
    String var1 = "231";
    String var2 = "Brezstel";
    for (int i = 0; i < 3; i++) {

        if (var0.equals(codesArrS[0][i])) {
            System.out.println("for var0=t we have var3 - " + codesArrS[2][i]);
        }
        if (var1.equals(codesArrS[1][i])) {
            System.out.println("for var1=231 we have var3 - " + codesArrS[1][i]);
        }
        if (var2.equals(codesArrS[2][i])) {
            System.out.println("for var2=Brezstel we have var3 - " + codesArrS[0][i]);
        }
    }
}

I used String 3D Array, but the problem is that it has fixed lenght. So I need to use some Collection, but can't find which one and how. As I now, List has only one parameter. Could someone, please, help me with the advice in which way to go or what is the better approach (maybe create a class and store objects...)?

...gratitude in advance!

Sergey Lotvin
  • 179
  • 1
  • 14
  • So you want a dynamic list of string lists? If I'm reading that correctly then a `List>` would suit your needs – PDStat Apr 05 '16 at 13:38
  • The other option is to write your own List. Java list is based on array, so you will make class, which will have "add" method. Steps how this method would work: 1) create new arraywhich would handle old elements + one new 2) copy elements from old array to new one 3) add your new element to array. – MyWay Apr 05 '16 at 13:38

2 Answers2

2

Create a class that contains the 3 values and store objects of this class in a List. That way your code will also get more readable.

List<Codes> codeList = new ArrayList<>();
    codeList.add(new Codes(" ", "231", "Ver"));
    codeList.add(new Codes("t", "12223", "Gutva2"));
    codeList.add(new Codes("n", "98745", "Brezstel"));

    String var0 = "t";
    String var1 = "231";
    String var2 = "Brezstel";

    for(Codes code : codeList) {

        if (var0.equals(code.getValue1())) {
            System.out.println("for var0=t we have var3 - " + code.getValue3());
        }
        if (var1.equals(code.getValue2())) {
            System.out.println("for var1=231 we have var3 - " + code.getValue2());
        }
        if (var2.equals(code.getValue3())) {
            System.out.println("for var2=Brezstel we have var3 - " + code.getValue1());
        }
    }
tak3shi
  • 2,305
  • 1
  • 20
  • 33
0

You could use a Array ArrayList if you have a fixed number of columns:

ArrayList<String>[] codesArrs = (ArrayList<String>[])new ArrayList[3];

so each list is one column

licklake
  • 236
  • 4
  • 15