Home > DeveloperSection > Forums > Check array in an arraylist contains a certain value
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    October-29-2014 12:49 AM

 Java Java 
Ratings:


 1 Reply(s)
 704  View(s)
Rate this:
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

}




Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    October-29-2014 12:54 AM

try this:

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

    }

    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):

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;

    }

 

}


Don't want to miss updates? Please click the below button!

Follow MindStick