首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java数组有关问题,循环有关问题

2013-01-11 
java数组问题,循环问题我用java连接数据库并将数据库中的两列数据存储成数组的形式调用结果在循环的时候只

java数组问题,循环问题
我用java连接数据库并将数据库中的两列数据存储成数组的形式调用
结果在循环的时候只是一直在最后一个数据里循环,请问这是为什么?
代码如下

List<HashStore> hashStores = new ArrayList();
HashStore hashStore = new HashStore();
// List<String> HC = new ArrayList<String>();
// List<String> ID = new ArrayList<String>();

//  public static void main(String[] args) 
  
  {
    try
    { Connection conn = null;

       Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
       String url ="jdbc:mysql://localhost/pic_manager?user=root&password=123&useUnicode=true&characterEncoding=gb2312"; 
       conn= DriverManager.getConnection(url); 
//       System.out.println("连接一次,连接成功!");   
       
       Statement stat = (Statement) conn.createStatement(); 
       
       ResultSet rs  =(ResultSet) stat.executeQuery("select * from pictures ");
       while (rs.next()){
       String a  =rs.getString(1);

       String d= rs.getString(10);
       
       hashStore.setNum(a);
       System.out.println(a);
       
       hashStore.setValue(d);
//       System.out.println(d);
    
       hashStores.add(hashStore);
       }

       rs.close();
       stat.close();
       conn.close();
      conn = null;
    }


这个是连接数据库,并封装数组的代码
conn k = new conn();

List<HashStore> hashCodes = k.aaa();

String sourceHashCode = "80f008f83b08cff0";

List<Integer> differences = new ArrayList<Integer>();


for (int i = 1; i < hashCodes.size(); i++)
    {    

    int difference = hammingDistance(sourceHashCode, hashCodes.get(i).getValue());
    differences.add(difference);
    System.out.println("一共有:"  +hashCodes.size());
    System.out.println("这张图片ID 是:" +hashCodes.get(i).getNum());
    System.out.println("这张图片ID 是:" +i);
if(difference==0 ){
System.out.println("这两张图片相同!");

}else if (difference<=5 ){
System.out.println("这两张图片相似!");
}else if (difference<=10 ){
System.out.println("这两张图片类似!");
}else {
System.out.println("这两张图片不相似!");
}

System.out.println(difference);//显示对比结果的值
    }



System.out.println(differences);

}

这个是调用,并循环的代码
8
9
10
11
19
20
21
22
23
24
25
26
一共有:12
这张图片ID 是:26
这张图片ID 是:1
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:2
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:3
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:4
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:5
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:6
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:7
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:8
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:9
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:10
这两张图片相同!
0
一共有:12
这张图片ID 是:26
这张图片ID 是:11
这两张图片相同!
0
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
这个是运行结果 java 数组 数组编号 array list
[解决办法]
看hashStore,每次加的都是同一个

hashStores.add(hashStore);
[解决办法]
在循环外边声明hashStore,在循环里面创建,这样保证存入的hashStore都是不一样的.

List<HashStore> hashStores = new ArrayList();
    HashStore hashStore=null;// ---------------只声明 new HashStore();
//     List<String> HC = new ArrayList<String>();
//     List<String> ID = new ArrayList<String>();
 
//  public static void main(String[] args) 
   
  {
    try
    {     Connection conn = null;
 
       Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
       String url ="jdbc:mysql://localhost/pic_manager?user=root&password=123&useUnicode=true&characterEncoding=gb2312"; 
       conn= DriverManager.getConnection(url); 
//       System.out.println("连接一次,连接成功!");   
        
       Statement stat = (Statement) conn.createStatement(); 
        
       ResultSet rs  =(ResultSet) stat.executeQuery("select * from pictures ");
           while (rs.next()){
               String a  =rs.getString(1);
 
               String d= rs.getString(10);
               hashStore = new HashStore();  //--------在这里创建,保证每次使用不同的hashStore对象。
                
               hashStore.setNum(a);


               System.out.println(a);
                
               hashStore.setValue(d);
//               System.out.println(d);
         
               hashStores.add(hashStore);
           }
 
           rs.close();
           stat.close();
           conn.close();
          conn = null;
    }


[解决办法]
还是放在List里,这样方便。
1 定义List<String[]> list=new ArrayList<String[]>();
2 定义二唯数组 String[] stringArray=null;
3 循环里创建 stringArray,即 stringArray=new String[2];
4 循环里赋值,stringArray[0]=a;
        stringArray[1]=d;
5 循环里放入容器list,即list.add(stringArray);

热点排行