• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Array of Hashtables :-How to display

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using array of hashtables.I am putting some key-value in the zeroth
position of the array hashtable and 1st position of the hashtable.My code is given belowi am getting the output as

0
Keys=w2 Value=0.2
Keys=w1 Value=0.1
Keys=w4 Value=0.4
Keys=w3 Value=0.3
************

but i want the value to be displayed as
0 //oth element of the array hashtable
Keys=w2 Value=0.2
Keys=w1 Value=0.1
1 //1st elemtn of the array hashtable
Keys=w4 Value=0.4
Keys=w3 Value=0.3
************



public class test
{
static Hashtable template= new Hashtable();
static Hashtable arrtable[]= new Hashtable[2];
public static void main(String args[])
{
int m=0;
template.put("w1", "0.1");
template.put("w2", "0.2");
arrtable[m]=template;
m++;
clear();
template.put("w3", "0.3");
template.put("w4", "0.4");
arrtable[m]=template;
clear();
displayValues();
}
public static void displayValues()
{
int m=0;
System.out.println(m);
for(Iterator n=arrtable[m].entrySet().iterator(); n.hasNext(); )
{
Map.Entry e = (Map.Entry) n.next();
System.out.println("Keys=" + e.getKey() + " Value=" + e.getValue());
m++;

}
System.out.println("************");

}
private static void clear()
{
Hashtable template = new Hashtable();

}

}
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print your variable m inside the for loop instead of out.
 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then this is the output

0
Keys=w2 Value=0.2
1
Keys=w1 Value=0.1
2
Keys=w4 Value=0.4
3
Keys=w3 Value=0.3
************

this is not what is to be desired i want the element of the array to be displayed one by one,like in zeroth position i have inserted w1,and w2.
0
Keys=w2 Value=0.2
Keys=w1 Value=0.1
1
Keys=w4 Value=0.4
Keys=w3 Value=0.3
************
 
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, because of incorrect scoping, your clear() method doesn't actually do anything.
 
Campbell Ritchie
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I presume your array of Maps is only for use as an exercise; I find it hard to see how this would be useful in real life!
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to do it in two nested loops (nesting one in another), as you are iterating through a collection of collections (collection here is used in general sense, not as java.util.Collection). Try something like that:


Haven't check if this compiles, but it should look like this.

BTW the code you posted is not very 'elegant'. Firstly, it is nice to declare your array as Map[] instead of Hashtable[]. Secondly, don't use Hashtable unless you need a synchronized collection (even in such case you may use Collections.synchronizedMap() instead). We are in the HashMap, LinkedHashMap and TreeMap age now Also, using the enhanced for loop (as in the example above) instead of explicitly iterators, makes your code somewhat more readable. And finally, you could replace


with one-line statement using anonymous inner class extending Hashtable (or HashMap ), but this is a bit long story

P.S. All (or almost all) of the above remarks are not relevant, if you're using Java 1.4.
 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this is a part of exercise.Thanks everyone for the suggestions and replies.The correct code would be as follows


import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

public class test
{
static Hashtable template= new Hashtable();
static Hashtable arrtable[]= new Hashtable[2];

public static void main(String args[])
{
int k=0;
template.put("w1", "0.1");
template.put("w2", "0.2");
arrtable[k]=template;

k++;
clear();

template.put("w3", "0.3");
template.put("w4", "0.4");
arrtable[k]=template;

displayValues();
}
public static void displayValue()
{


for (int m = 0; m < arrtable.length; m++)
{

System.out.println(m);
for(Iterator n=arrtable[m].entrySet().iterator(); n.hasNext(); )
{

Map.Entry e = (Map.Entry) n.next();
System.out.println("Keys=" + e.getKey() + " Value=" + e.getValue());

}
System.out.println("************");
}


}
private static void clear()
{
template = new Hashtable();
}

}

You dont have to redeclare the reference variable in clear() method.just make a new object :-) and also one outer loop to parse one by one the elements of the hashtable array:-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic