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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26255862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:35:05  来源:igfitidea点击:

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

javaarraysarraylistcontains

提问by 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).

我知道这行不通,但我现在不知道该怎么做。我已经在 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?).

注意:我在数组列表中选择了这种数组结构,因为我有固定数量的列(如如何在 java 中创建动态二维数组中所建议的)。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by 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:

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

    }

}

Here is an example with two classes (Transactionand 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:

交易线:

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

}

回答by assylias

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

回答by Toro Boro

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 提供的解决方案是正确的

回答by Chet M.

I got your point. By using ArrayList you are trying to make an array of another array of strings. But you have made one simple mistake.This is how you tried to retrieved a String inside an array inside an ArrayList:

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

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). Instead this is what you should have done:

这个“某个值”是字符串数组“transactionLine”中存在的字符串,而不是由列表“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
  }

Hope this helps.

希望这可以帮助。