2 Replies Latest reply: Jun 28, 2010 10:07 AM by 843793 RSS

    simple question

    843793
      hello there,,
                     ArrayList[][] al = new ArrayList[resultset.getRow()][3];
                          
                          int count = 0;
                          while(resultset.next())
                          {
                               List returnVar = new ArrayList();
                               returnVar.add(0, resultset.getInt("user_id"));
                               returnVar.add(1, resultset.getString("fullname"));
                               returnVar.add(2, resultset.getString("title"));
                               ht.put(count, returnVar);
                               count++;
                          }
                
      as you can see i initialized arraylist(al) and to be put() to Hashtable(ht)
      but when i return the Hashtable(ht) to the method caller and parses it
                Hashtable users = sd.getUsers(rowCount); // the method i shown above
                for(int i = 0; i < users.size(); i++)
                {
                     Hashtable ht = new Hashtable();
                     List aaa = Arrays.asList(users.get(i));
                     for(int a = 0; a < aaa.size(); a++)
                     {
                          Object xyz = aaa.get(a);
                          System.out.println(aaa.get(a) + " " + a);
                     }     
                }
                
      it returns an object { value1, value2, value3 }
      my question is how can i parse these by index??

      aaa[0] = value1
      aaa[1] = value2
      aaa[2] = value3
        • 1. Re: simple question
          Kayaman
          Why would you use a Hashtable when you want to access it by index?

          But if you really want to write bizarre code like that, you can always loop through numbers (since that's what your keys are) and just get them from the Hashtable normally.


          I'm amazed that you're writing SQL code there, yet you don't know how collections work.
          • 2. Re: simple question
            843793
            You might want to write like this
            List myList = new ArrayList();
            while(rs.next()){
            myList.add(new MyObject(resultset.getInt("user_id"), resultset.getInt("fullname"), resultset.getInt("title"));
            }
            
            int myListSize = myList.size();
            int i;
            MyObject myObject;
            
            for(i=0;i<myListSize;i++){
            myObject = (MyObject) myList.get(i);
            System.out.println(myObject.getUserID());
            System.out.println(myObject.getFullName());
            System.out.println(myObject.getTitle());
            }