Java-检查Integer数组列表是否包含大于特定值的元素的条件

时间:2018-09-13 06:19:36

标签: java collections break

我已更新代码。我有3个arraylist,并根据要更新值的条件。我正在使用每个循环进行迭代,但是现在我在中断时做错了什么,它不是重置/获取type和len的下一个值。只是重置名称。

 List <String> columnNameList = new ArrayList <String>();
    List <String> columnTypeList = new ArrayList <String>();
    List <Integer> columnLengthList = new ArrayList <Integer>();
    String result = "";

                    columnNameList.add("Id");
                    columnNameList.add("Name");
                    columnNameList.add("Address");

                    columnTypeList.add("char");
                    columnTypeList.add("varchar");
                    columnTypeList.add("varchar");


                    columnLengthList.add(18);
                    columnLengthList.add(50);
                    columnLengthList.add(10000);




                    outermostloop: for (String name : columnNameList )
                    { 
                        outerloop:  for (String type : columnTypeList)
                        {
                            loop: for (int len : columnLengthList)
                            {

                                if(len > 0 && (!type.equalsIgnoreCase("int") && !type.equalsIgnoreCase("datetime") && !type.equalsIgnoreCase("datetime2")))
                                        {

                                    if(len > 8000 && !(name.equalsIgnoreCase("Id")) && (type.equalsIgnoreCase("varchar")))
                                    {
                                        result =  name + type + "(max) ";
                                        System.out.println("if len > 8000 && name not id and type is varchar " + result);  
                                        // O/P expected : Address  varchar(max)
                                    }

                                    else
                                    {
                                        String finalres = name + type + "("+ len +") ";
                                        System.out.println("No conversion " + finalres);    
                                        /* O/P expected : Id char(18)
                                                         Name varchar(50) 
                                         */

                                    }

                                        }

                                break outerloop;


                            } 

                        }  

                    }

But what I am getting with above logic :
No conversion Id char (18) 
No conversion Name char (18) 
No conversion Address char (18)

3 个答案:

答案 0 :(得分:3)

您必须遍历List的元素,直到找到大于8000的元素。

使用Stream,您可以使用以下命令编写它:

if (columnLengthList.stream().anyMatch(i -> i > 8000)) {

} else {

}

答案 1 :(得分:1)

这可能会有所帮助:

public static void main(String[] args) throws Exception {

    ArrayList<Integer> ar1 = new ArrayList<>();
    ar1.add(1);
    ar1.add(2);
    ar1.add(3);
    ar1.add(4);
    ar1.add(5);

    int particular_value = 3;

    for (int i : ar1) {
        if (i > particular_value)
            System.out.println("Value " + i + " is greater than " + particular_value);
    }

}

答案 2 :(得分:1)

您必须遍历列表。使用流或扩展的for循环或常规的for循环。 例如:

List <Integer> columnLengthList = new ArrayList <Integer>();
        columnLengthList.add(8001);
        columnLengthList.add(7000);

        for (Integer eachElement: columnLengthList) {
            if (eachElement > 8000) {
                // run these 
            } else {
                // run these
            }
        }