从ResultSet SQL结果填充数组

时间:2012-04-03 13:10:32

标签: java multidimensional-array arraylist

我从数据库中检索了单个String值,并使用String方法split()将其转换为String数组。

ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
while(set.next())
{
    num1 = set.getString(1);                   
    num2 = set.getString(2);
    num3 = set.getString(3);
    num4 = set.getString(4);
    num5 = set.getString(5);
    num6 = set.getString(6);
    num7 = set.getString(7);
    totalLotto += num1 + " " + num2 + " " + num3 + " " +  num4 + " " +
        num5 + " " +  num6 + " " +  num7 + " " + "/";
}
String[]listOfNumbers = totalLotto.split("/");

结果输出如下

3 18 27 38 41 45 47     
4 7 11 15 22 33 42
2 9 15 23 24 39 44 
4 11 16 17 35 39 48

如何将上述数字添加到数组的数组中,以便我可以遍历每个单独的数字?(并随后检查它们是否包含在中奖号码列表中?)

亲切的问候

7 个答案:

答案 0 :(得分:4)

你为什么要使用字符串?这似乎是数字,所以整数将是一个更好的匹配:

List<int[]> listOfNumbers = new ArrayList<int[]>();
while(set.next()) {
     int[] numbers = new int[7];
     for (int i = 0; i < numbers.length; i++) {
         numbers[i] = set.getInt(i + 1);
     }
     listOfNumbers.add(numbers);
}

如果有必要,你显然也可以使用Integer[]

答案 1 :(得分:3)

您可以将resultSet中的值分配给字符串数组并将其添加到列表中。

示例:

ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
List<String[]> lottoList = new LinkedList<String[]>();
while(set.next())
{
  String[] currentRow = new String[] {set.getString(1),
                                      set.getString(2), 
                                      set.getString(3), 
                                      set.getString(4), 
                                      set.getString(5), 
                                      set.getString(6),  
                                      set.getString(7)};
  lottoList.add(currentRow);
}
// do whatever you like with lottoList

然后你可以循环遍历lottoList并查看有多少中奖号码。

答案 2 :(得分:2)

您可以使用ArrayListString数组:

ArrayList<String[]> numbers = new ArrayList<String[]>();

然后进一步按空格分割listOfNumbers[0]并将结果添加到集合中:

String[] listOfNumbers = totalLotto.split("/");
numbers.add(listOfNumbers[0].split(" "));

假设listOfNumbers[0]

3 18 27 38 41 45 47

按空格拆分会为您提供String[],每个位置都有一个数字。

答案 3 :(得分:2)

ResultSet set = state.executeQuery("SELECT COUNT(*) FROM numberLotto");
String[][] array = new String[rs.getInt(1)][6];

ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
int i = 0;
    while(set.next())
    {

                array[i][0] = set.getString(1);                   
                array[i][1]= set.getString(2);
                array[i][2]= set.getString(3);
                array[i][3]= set.getString(4);
                array[i][4]= set.getString(5);
                array[i][5]= set.getString(6);
                array[i][6]= set.getString(7);
          i ++;     
    }

答案 4 :(得分:2)

       List<int[]> list = new ArrayList<int[]>();
        while (set.next()) {
            int[] array = new int[7];
            for (int i = 1; i <= 7; i++) {
                array[i - 1] = set.getInt(i);
            }

            list.add(array);
        }

答案 5 :(得分:1)

这是一个示例,您可以将其更改为您的代码样式

String[] a = {"10 20 30 40 50","100 200 300 400 500","1000 2000 3000 4000"};
          String[][] b = new String[a.length][];
          for(int i = 0; i<a.length; i++){
              b[i] = a[i].split(" ");
          }

答案 6 :(得分:1)

ResultSet set = state.executeQuery("SELECT * FROM numberLotto");
List<String[]> tmp = new ArrayList<String[]>();
while(set.next())
{
    String[] row = new String[7];
    row [0] = set.getString(1);                   
    row [1] = set.getString(2);
    row [2] = set.getString(3);
    row [3] = set.getString(4);
    row [4] = set.getString(5);
    row [5] = set.getString(6);
    row [6] = set.getString(7);
    tmp.add(row);
}
String[][] listOfNumbers = tmp.toArray(new String[tmp.size()][7]);