Java如何遍历3 arrayList

时间:2018-09-13 12:01:03

标签: java collections break

我创建了三个ArrayList并为其添加了一些值。然后,我遍历每个ArrayList,并希望根据if语句的条件更新值。下面是我到目前为止已实现的代码,但没有给我预期的输出:

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


更新代码

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) {
 */
for (int i = 0; i < columnNameList.size(); i++)
{
    for (i = 0; i < columnTypeList.size(); i++)
    {
        for (i = 0; i < columnLengthList.size(); i++)
        {
            if(columnLengthList.get(i) > 0 && (!columnTypeList.get(i).equalsIgnoreCase("int") && !columnTypeList.get(i).equalsIgnoreCase("datetime") && !columnTypeList.get(i).equalsIgnoreCase("datetime2")))
            {
                if(columnLengthList.get(i) > 8000 && !(columnNameList.get(i).equalsIgnoreCase("Id")) && (columnTypeList.get(i).equalsIgnoreCase("varchar")))
                {
                    result = columnNameList.get(i) + " " + columnTypeList.get(i) + "(max) ";
                    System.out.println("if len > 8000 && name not id and type is varchar " + result);
                    // O/P expected : Address  varchar(max)
                }
                else
                {
                    String finalres = columnNameList.get(i) + " " + columnTypeList.get(i) + "(" + columnLengthList.get(i) + ") ";
                    System.out.println("No conversion " + finalres);
                    /* O/P expected : Id char(18)
                                      Name varchar(50)
                    */
                }
            }
        }
    }
}

当前输出:

No conversion Id char(18) 
No conversion Name char(18) 
No conversion Address char(18)

预期输出:

No conversion Id char(18) 
No conversion Name varchar(50)
if len > 8000 && name not id and type is varchar -> Address varchar(max)

我认为break做错了。有人可以让我知道当前代码有什么问题吗?还是我缺少什么?

0 个答案:

没有答案