简体   繁体   English

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

[英]Java - If condition to check if an Integer array-list contains element which is greater than particular value

I have updated my code.我已经更新了我的代码。 I have 3 arraylists and based on the condition I want to update values.我有 3 个数组列表,并根据我想更新值的条件。 I am using for each loop to iterate but now I am doing something wrong with the break it is not resetting/picking up next value for type and len.我正在使用 for 每个循环进行迭代,但现在我在中断时做错了,它没有重置/获取 type 和 len 的下一个值。 It is only resetting name.它只是重置名称。

 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)

You have to iterate over the elements of the List until you find an element larger than 8000.您必须遍历List的元素,直到找到大于 8000 的元素。

With Stream s you can write it with:使用Stream您可以使用以下命令编写它:

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

} else {

}

This might help:这可能有帮助:

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

}

You have to iterate over the list.您必须遍历列表。 Either using streams or an extended for loop or a general for loop.使用流或扩展的 for 循环或通用的 for 循环。 For ex:例如:

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

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

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

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