CHECK ARRAY IN AN ARRAYLIST CONTAINS A CERTAIN VALUE

Norman Reedus

Total Post:45

Points:315
Posted by  Norman Reedus
Java 
 1366  View(s)
Ratings:
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
}


  1. Manoj Bhatt

    Post:154

    Points:1086
    Re: Check array in an arraylist contains a certain value

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

      Modified On Apr-06-2018 01:33:38 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!