如何检查arraylist中的数组是否包含某个值?

时间:2014-10-08 11:31:17

标签: java arrays arraylist contains

我有一个数组列表,其中包含String类型的数组。我使用以下代码创建数组列表并向其添加数组:

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

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

transactionLines.add(transactionLine);

现在我想测试其中一个数组是否包含某个值。我尝试了这样,但它检查了一个数组,而不是数组的元素:

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

我知道这不起作用,但我现在不知道如何做到这一点。我无法在Stackoverflow上找到任何此帖子(无论如何都没有找到此问题的逻辑搜索术语。)

注意:我在arraylist中选择了这种数组结构,因为我有一定​​数量的列(如how to create dynamic two dimensional array in java?中所述)。

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:7)

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

    }

}

以下是两个类(TransactionTransactionLine)的示例:

<强>测试

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

}

答案 1 :(得分:5)

解决问题的面向对象方法是创建一个类:

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

然后,如果您需要测试列表中是否有给定的交易,您可以在该类中实施equalshashcode,这样您就可以调用:

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

如果您只需要查找具有特定特征的事务,则需要遍历列表并检查每个事务,例如:

Transaction result = null;
for (Transaction t : transacionLines) {
  if(t.getName().equals("some value") {
    result = t;
    break;
  }
}

答案 2 :(得分: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;
}

提供的代码完全符合您的要求,但@assylias提供的解决方案是正确的

答案 3 :(得分:0)

我明白了你的意思。通过使用ArrayList,您尝试创建另一个字符串数组的数组。但是你犯了一个简单的错误。这就是你试图在ArrayList中的数组中检索一个String的方法:

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

这个“某个值”是字符串数组“transactionLine”中的字符串,而不是List“transactionLines”(指的是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
		}

希望这有帮助。