4

I have an array list which contains arrays of type String. I create the array list and add arrays to it with the following code:

List<String[]> transaction = new ArrayList<String[]>();

String[] transactionLine = new String[7];
transactionLine[0] = "0";
transactionLine[1] = "1";
//.....
transactionLine[6] = "some value";

transactionLines.add(transactionLine);

Now I want to test if one of the arrays contain a certain value. I tried it like this, but then it checks for an array and not an element of an array:

if(transactionLines.contains("some value")) { 
     //Do some stuff with it
}

I know this doesn't work, but I don't now how to do it otherwise. I couldn't find any post of this already on Stackoverflow (not with the logical search terms for this problem anyway).

Note: I have chosen this structure of arrays in an arraylist, because I have a fixed number of columns (as suggested in how to create dynamic two dimensional array in java?).

Any help is greatly appreciated!

Community
  • 1
  • 1
bashoogzaad
  • 4,611
  • 8
  • 40
  • 65

4 Answers4

6

@assylias suggestion to use the object oriented way is good, but his example does not tell if the list contains a transaction where one property has a certain value. This example does:

public class Test {

    public static void main(final String[] args) {
        final List<TransactionLine> transaction = new ArrayList<>();

        transaction.add(new TransactionLine(1, "some value"));
        transaction.add(new TransactionLine(2, "another value"));
        transaction.add(new TransactionLine(3, "yet another value"));

        System.out.println(containsName(transaction, "some value"));
        System.out.println(containsName(transaction, "non-existent value"));
    }

    // Iterates over all transactions until a transaction is found that has the
    // same name as specified in search
    private static boolean containsName(final List<TransactionLine> transaction, final String search) {
        for (final TransactionLine transactionLine : transaction) {
            if (transactionLine.getName().equals(search)) {
                return true;
            }
        }

        return false;
    }

    private static class TransactionLine {

        private int id;

        private String name;

        public TransactionLine(final int id, final String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(final int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

    }

}

Here is an example with two classes (Transaction and TransactionLine):

Test:

public class Test {

    public static void main(final String[] args) throws Exception {
        final Transaction transaction = new Transaction();

        transaction.add("some name");
        transaction.add("another name");
        transaction.add("yet another name");

        System.out.println(transaction.containsName("some name"));
        System.out.println(transaction.containsName("non-existent name"));
    }

}

Transaction:

import java.util.ArrayList;
import java.util.List;

public class Transaction {

    private final List<TransactionLine> transactionLines = new ArrayList<>();

    public void add(final String name) {
        final TransactionLine tl = new TransactionLine(transactionLines.size(), name);

        transactionLines.add(tl);
    }

    public boolean containsName(final String name) {
        for (final TransactionLine transactionLine : transactionLines) {
            if (transactionLine.getName().equals(name)) {
                return true;
            }
        }

        return false;
    }

}

TransactionLine:

public class TransactionLine {

    private int id;

    private String name;

    public TransactionLine() {
    }

    public TransactionLine(final int id, final String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(final int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

}
stevecross
  • 5,588
  • 7
  • 47
  • 85
  • Thank you for your answer. You have made a class Transaction, but could you please use the names of the variables as I named them? It is quite confusing for me now if I have to make a class Transaction or TransactionLine. (A transaction consists of multiple transactionLines). – bashoogzaad Oct 08 '14 at 12:02
  • Well then just name the class `TransactionLine` and the list `transaction`. But then it would be better to wrap the list in an object named `Transaction` again. Then you could implement the method `containsName()` as an instance method of this new class. – stevecross Oct 08 '14 at 12:08
  • Oke, thank you. I will be able to create this. Thanks all of you for helping a new-to-java programmer. – bashoogzaad Oct 08 '14 at 12:10
5

The object oriented way of solving your problem would be to create a class:

class Transaction {
    private final int id;
    private final String name;
    //etc.
}

Then if you need to test if a given transaction is in the list you could implement equals and hashcode in that class, which would enable you to call:

if(transactionLines.contains(someTransaction)) { ... }

If you just need to find transactions with a specific characteristics, you would need to iterate over the list and check each transaction, for example:

Transaction result = null;
for (Transaction t : transacionLines) {
  if(t.getName().equals("some value") {
    result = t;
    break;
  }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • This would check if the list contains a whole transaction. But he wants to know if the list contains a transaction where a member has a certain value. – stevecross Oct 08 '14 at 11:45
  • @assylias Thank you for your answer, greatly appreciated. I would like to do it the object oriented way, but I don't know what you mean with "implement equals and hashcode". Could it be possible to give a more detailed code example or some source where I can find something to help solving this problem? Thanks in advance! – bashoogzaad Oct 08 '14 at 11:55
4
public static boolean isListOfStringArraysContainsString(List<String[]> arrayList, String s) {
    for (String[] arr : arrayList) {
        for (String string : arr) {
            if ((string != null) && (string.equals(s))) {
                return true;
            }
        }
    }
    return false;
}

Provided code do exactly what you are asking about, but solution provided by @assylias is proper

Toro Boro
  • 377
  • 5
  • 16
  • Thank you for your answer! I will try it, but if you also say it is better to do it the object oriented way, I will. – bashoogzaad Oct 08 '14 at 11:56
0

I got your point. By using ArrayList you are trying to make an array of another array of strings. But you have made one simple mistake.This is how you tried to retrieved a String inside an array inside an ArrayList:

if(transactionLines.contains("some value")) { 
     //Do some stuff with it
}

This "some value" is a string present in String array "transactionLine" and not referred by the List "transactionLines" (which is referring to ArrayList object). Instead this is what you should have done:

List<String[]> transactionLines = new ArrayList<String[]>();
  
  String[] transactionLine = new String[7];
  transactionLine[0] = "0";
  transactionLine[1] = "1";
  transactionLine[2] = "something";
  transactionLine[3] = "3";
  transactionLine[4] = "4";
  
  transactionLines.add(transactionLine);
  
  String[] mySL=transactionLines.get(0);
  
  System.out.println(mySL[2]);
  if (mySL[2].equals("something")) {
   //some code
  } else {
   //some code
  }

Hope this helps.