Question

Good Evening, I try to use the HashTable() for storing a database table records temporarily . The problem is I dunno why the records which being put inside the HashTable() will always be the first record. I think the problem occur is because of the wrong for loops concept, related code:

Declare

Hashtable hashsample = new Hashtable();

for loop

for (i = 0; i< db.getNumberOfRows(); i++) {

        hashsample.put(i, db.getData());
        System.out.println(hashsample);
    }

p/s: I'm new in HashTable, the db (database statement) run fine...

Need some hints and advised, thanks in advanced^^

Était-ce utile?

La solution

I tried your code mocking the db:

public static void main(String[] args) {

    HashMap db = new HashMap();
    db.put(0, "zero");
    db.put(1, "one");
    db.put(2, "two");
    db.put(3, "three");

    Hashtable hashsample = new Hashtable();

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

            hashsample.put(i, db.get(i));
            System.out.println(hashsample);
    }

}

and it works just fine. Here the output:

{0=zero}
{1=one, 0=zero}
{2=two, 1=one, 0=zero}
{3=three, 2=two, 1=one, 0=zero}

The problem I think is that your db.getData() return always the same thing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top