简体   繁体   中英

Java how to iterate through 3 arrayList

I've created three ArrayList s and added a few values to them. I am then iterating over each of the ArrayList s and want to update the values based on the conditions of the if statements. Below is the code I have implemented so far, but it does not give me expected output:

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


Updated Code

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)
                    */
                }
            }
        }
    }
}

Current Output:

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

Expected Output:

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

I think I am doing something wrong with break . Can anyone please let me know what's wrong in the current code or is there anything I am missing?

It does not work because of the structure of your loops. outermostloop starts with "Id". outerloop starts with "char". loop starts with 18. Now your if-clauses are getting evaluated. Afterwards you break outerloop. Now outermostloop goes on to "Name". But outerloop starts from the beginning again, using "char". Same goes for loop.

You could just replace the current loops with

for(int i = 0; i < columnNameList.size(); i++){

and then replace:

name --> columnNameList.get(i)
type --> columnTypeList.get(i)
len --> columnLengthList.get(i)

Another option would be to wrap all three parameters into your own class and then iterate over an ArrayList of this class. Example:

private class Column{
    public String name;
    public String type;
    public int len;
    public Column(String name, String type, int len){
        this.name=name;
        this.type=type;
        this.len=len;
    }
}

Then use

ArrayList<Column> columns

and

columns.add(new Column("Id","Char",18));

and so on... Access replacing

name --> column.name

when using the loop

for(Column column: columns){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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