Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Value store confused....

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);


}

}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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.
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get an exception when I run the code with three command line parameters. Again - show us your command line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic