How to check if an array in arraylist contains a specific value?

I have a list of arrays containing arrays of type String. I create a list of arrays 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 check if one of the arrays contains a specific value. I tried it like this, but then it checks the array, not the array element:

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

I know that this does not work, but I do not understand how to do it otherwise. I could not find a single message about this already in Stackoverflow (but not with logical search terms for this problem).

Note. I chose this array structure in arraylist because I have a fixed number of columns (as suggested in how to create a dynamic two-dimensional array in java? ).

Any help is much appreciated!

+4
4

@assylias - , , , . :

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;
        }

    }

}

(Transaction TransactionLine):

:

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"));
    }

}

:

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;
    }

}
+6

- - :

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

, , , equals hashcode , :

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

, , :

Transaction result = null;
for (Transaction t : transacionLines) {
  if(t.getName().equals("some value") {
    result = t;
    break;
  }
}
+5
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;
}

, , , @assylias,

+4

. ArrayList, . . ArrayList:

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

" " , String "transactionLine" " " ( ArrayList). :

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
		}
Hide result

, .

0

All Articles