循环通过2D阵列

时间:2016-10-15 16:01:12

标签: java

我想在2D字符串数组中保存两个字符串,并进一步将其用于其他方法。

我的数组:

String[][] x = {{"Iron Ice Tea", "5"}, {"Clock Clock", "3"}, {"Shoe Telephone", "3"}};

我的方法:

  public static String getInsertedCustomerOrder(String[][] input) {
        String query = "";
        int x = 0;

    for (String[] product : input) {
        for (String count : product) {
            if (x == 0) {
            query = query + "((SELECT PRICE FROM PRODUCT WHERE NAME = '" + product + "') * " + count + " + ";
            } else {
            query = query + "(SELECT PRICE FROM PRODUCT WHERE NAME = '" + product + "') * " + count + " + ";
            }
            x++;
        }
    }

    if (query != null && query.length() > 0 && query.charAt(query.length() - 2) == '+') {
        query = query.substring(0, query.length() - 2);
    }

    query = query + ")";

    return query;
}

输出结果为:

((SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@1db9742') * Iron Ice Tea + (SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@1db9742') * 5 + (SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@106d69c') * Clock Clock + (SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@106d69c') * 3 + (SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@52e922') * Shoe Telephone + (SELECT PRICE FROM PRODUCT WHERE NAME = '[Ljava.lang.String;@52e922') * 3 )

它应该是:

((SELECT PRICE FROM PRODUCT WHERE NAME = 'Iron Ice Tea') * 5))

2 个答案:

答案 0 :(得分:0)

试试这个。

public static String getInsertedCustomerOrder(String[][] input) {
    boolean first = true;
    String query = "";
    for (String[] product : input) {
        if (first)
            first = false;
        else
            query += " + ";
        query += "(SELECT PRICE FROM PRODUCT WHERE NAME = '" + product[0] + "') * "
               + product[1];
    }
    query = "(" + query + ")";
    return query;
}

String[][] input = { { "Iron Ice Tea", "5" }, { "Clock Clock", "3" }, { "Shoe Telephone", "3" } };
System.out.println(getInsertedCustomerOrder(input));

结果:

((SELECT PRICE FROM PRODUCT WHERE NAME = 'Iron Ice Tea') * 5 + (SELECT PRICE FROM PRODUCT WHERE NAME = 'Clock Clock') * 3 + (SELECT PRICE FROM PRODUCT WHERE NAME = 'Shoe Telephone') * 3)

答案 1 :(得分:0)

Could you please try below code snippet -

        String[][] x = {{"Iron Ice Tea", "5"}, {"Clock Clock", "3"}, {"Shoe Telephone", "3"}};


        int lastIndex = x.length-1;
        int currentIndex = 0 ;

        String query = "";

        for (String[] product : x) {

            if(currentIndex == lastIndex)
                query +="((SELECT PRICE FROM PRODUCT WHERE NAME = '"+product[0]+"') * "+product[1]+")";
            else
                query +="((SELECT PRICE FROM PRODUCT WHERE NAME = '"+product[0]+"') * "+product[1]+") + ";
            currentIndex++;
        }


        System.out.println("query : "+query);

product

x[0] = {"Iron Ice Tea", "5"} - is object so will get object hash code

x[1] = {"Clock Clock", "3"} - is object will get object hash code

x[2] = {"Shoe Telephone", "3"} - is object will get object hash code


first iteration

product[0] = x[0][0] = "Iron Ice Tea" , product[1] = x[0][1] = "5"

second iteration

product[0] = x[1][0] = "Clock Clock" , product[1] = x[1][1] = "3"

third iteration

product[0] = x[2][0] = "Shoe Telephone" , product[1] = x[2][1] = "3"