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

bashoogzaad

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!

stevecross

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

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IOS how to check the value of an array if it contains a certain value and display it out

From Dev

How to check dictionary contains certain value, not certain key?

From Dev

How to check dictionary contains certain value, not certain key?

From Dev

PHP: how to assert that an array contains a certain value?

From Dev

How to check if the multidimensional array contains certain key and value pair on all "levels"?

From Dev

How to check if the multidimensional array contains certain key and value pair on all "levels"?

From Dev

JavaScript/jQuery check that array contains all a certain value?

From Dev

check if dictionary object in array contains certain value in javascript

From Dev

Check if ArrayList contains an Array Object

From Dev

How to check if an array index contains a value

From Dev

PHP - How to check if array contains a value by index?

From Dev

How to check if multidimensional array contains same value?

From Dev

How to check if entity in set contains a certain value using criteriaBuilder?

From Dev

How to check if XML string contains certain value in jQuery

From Dev

Javascript: How can i check if a array contains certain values

From Dev

How do i check if an Array List contains a certain string

From Dev

How to check if a string contains a certain element from an array, but not the other elements

From Dev

How to check if TreeMap<String, ArrayList<String>> contains a value? in Java

From Dev

how to check if arraylist contains a string

From Dev

how to use sql to Check if a certain column value contains another certain column value

From Dev

jQuery: check if column contains certain value

From Dev

jQuery: check if column contains certain value

From Dev

how to check if a value inside an array is larger than a certain value

From Dev

How can I check each row of a large csv and write the row if a certain column contains a certain value?

From Java

Check if string contains a value in array

From Dev

Check if an awk array contains a value

From Dev

Check if array contains value in angularjs

From Dev

Check if array contains specific value

From Dev

Javascript check if array contains value

Related Related

  1. 1

    IOS how to check the value of an array if it contains a certain value and display it out

  2. 2

    How to check dictionary contains certain value, not certain key?

  3. 3

    How to check dictionary contains certain value, not certain key?

  4. 4

    PHP: how to assert that an array contains a certain value?

  5. 5

    How to check if the multidimensional array contains certain key and value pair on all "levels"?

  6. 6

    How to check if the multidimensional array contains certain key and value pair on all "levels"?

  7. 7

    JavaScript/jQuery check that array contains all a certain value?

  8. 8

    check if dictionary object in array contains certain value in javascript

  9. 9

    Check if ArrayList contains an Array Object

  10. 10

    How to check if an array index contains a value

  11. 11

    PHP - How to check if array contains a value by index?

  12. 12

    How to check if multidimensional array contains same value?

  13. 13

    How to check if entity in set contains a certain value using criteriaBuilder?

  14. 14

    How to check if XML string contains certain value in jQuery

  15. 15

    Javascript: How can i check if a array contains certain values

  16. 16

    How do i check if an Array List contains a certain string

  17. 17

    How to check if a string contains a certain element from an array, but not the other elements

  18. 18

    How to check if TreeMap<String, ArrayList<String>> contains a value? in Java

  19. 19

    how to check if arraylist contains a string

  20. 20

    how to use sql to Check if a certain column value contains another certain column value

  21. 21

    jQuery: check if column contains certain value

  22. 22

    jQuery: check if column contains certain value

  23. 23

    how to check if a value inside an array is larger than a certain value

  24. 24

    How can I check each row of a large csv and write the row if a certain column contains a certain value?

  25. 25

    Check if string contains a value in array

  26. 26

    Check if an awk array contains a value

  27. 27

    Check if array contains value in angularjs

  28. 28

    Check if array contains specific value

  29. 29

    Javascript check if array contains value

HotTag

Archive