简体   繁体   English

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

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

I have an array list which contains arrays of type String. 我有一个数组列表,其中包含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). 我在Stackoverflow上找不到任何这个帖子(无论如何都没有这个问题的逻辑搜索术语)。

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? ). 注意:我在arraylist中选择了这种数组结构,因为我有一个固定数量的列(如在java中创建动态二维数组的建议 )。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

@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. @assylias建议使用面向对象的方式很好,但是他的示例并没有告诉列表是否包含一个属性具有特定值的事务。 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 ): 以下是两个类( TransactionTransactionLine )的示例:

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

}

The object oriented way of solving your problem would be to create a class: 解决问题的面向对象方法是创建一个类:

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

Then if you need to test if a given transaction is in the list you could implement equals and hashcode in that class, which would enable you to call: 然后,如果您需要测试给定事务是否在列表中,您可以在该类中实现equalshashcode ,这将使您能够调用:

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

If you just need to find transactions with a specific characteristics, you would need to iterate over the list and check each transaction, for example: 如果您只需要查找具有特定特征的事务,则需要遍历列表并检查每个事务,例如:

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

Provided code do exactly what you are asking about, but solution provided by @assylias is proper 提供的代码完全按照您的要求进行,但@assylias提供的解决方案是正确的

I got your point. 我明白了你的意思。 By using ArrayList you are trying to make an array of another array of strings. 通过使用ArrayList,您尝试创建另一个字符串数组的数组。 But you have made one simple mistake.This is how you tried to retrieved a String inside an array inside an ArrayList: 但是你犯了一个简单的错误。这就是你试图在ArrayList中的数组中检索一个String的方法:

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

This "some value" is a string present in String array "transactionLine" and not referred by the List "transactionLines" (which is referring to ArrayList object). 这个“某个值”是字符串数组“transactionLine”中的字符串,而不是List“transactionLines”(它指的是ArrayList对象)。 Instead this is what you should have done: 相反,这是你应该做的:

 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 } 

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM