File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Value store confused.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Value store confused...." Watch "Value store confused...." New topic
Author

Value store confused....

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
What happens when the Junk2 is executed?How the execution proceeds?I'm a bit confused about what happens at (1),(2),(3) during execution?How and where the values are stored?

public class Junk1 {
public Junk1(){
hm = new HashMap[1]; ...............(1)
hm[0] = new HashMap(); ..............(2)
}
public boolean put(Object key,Object value){
if(value !=null){
hm[current].put(key, value);.........(3)

return true;
}
else return false;
}
HashMap hm[];
private int current =0;
}

---------------------------------------------------------------------------

public class Junk2 {


public static void main(String[] args) {
Junk1 jn = new Junk1();
jn.put("emp_id", args[0]);
jn.put("name", args[1]);
jn.put("password",args[2]);


}

}


Thanks,
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
(1) creates an array of HashMaps of length 1.
(2) sets the first (and only) element of this array to reference a new Hashmap object
(3) adds an entry to the Hashmap created in (2)

Junk2.main stores 3 entries in this HashMap using the Junk1.put() method.


Joanne
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
when executing the code why do I get ArrayOutOfBounds Exception at
data[currentRow].put(key, fld);

Since we have created a HashMap Object at data[0] = new HashMap(),with .put(Object key,Object value) command inserts an entry for the HashMap Object.
So why the ArrayOutOfBounds Exception?

HashMap object created gets alloted a default size.Is it?.


The above I tried executing in MyEclipse6.0 and received error in console.

Request help on this.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
What command line are you using to run the program ? If you don't pass three arguments then you will get an out of bounds exception when you access args[0], args[1] or args[2] (depending on how many arguments you do pass).
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
There isn't a data[currentRow].put(key, fld); line in your code.

It's best to post code that compiles - more people will help you.
You should also use the CODE tags - look for the buttons below the editing area titled Instant UBB Code.

But you should get an

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Junk1.main

if there are no arguments.

Here's your cleaned up code (also using generics from Java 1.5), reposted:


I'm not sure why you'd ever want an array of Maps. If you really want multiple maps use an ArrayList.

ArrayList<Map<String, String>> collectionOfMaps;


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
public class Junk1 {
public Junk1(){
hm = new HashMap[1]; ...............(1)
hm[0] = new HashMap(); ..............(2)
}
public boolean put(Object key,Object value){
if(value !=null){
hm[current].put(key, value);.........(3)

return true;
}
else return false;
}
HashMap hm[];
private int current =0;
}

---------------------------------------------------------------------------

public class Junk2 {


public static void main(String[] args) {
Junk1 jn = new Junk1();
jn.put("emp_id", args[0]);
jn.put("name", args[1]);
jn.put("password",args[2]);


}

}

On execution of the above program with three arguments passed the below error is displayed.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Junk1.main

at line no 3.

Why is it so?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
I don't get an exception when I run the code with three command line parameters. Again - show us your command line.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value store confused....