1

I'm trying to put together a hashmap with a char array as the value and a string as the key. I'm trying to print out the Keys and Values but keep getting an array out of bounds exception. I'm not sure what the problem is. (Also I might be trying to print out the map incorrectly; I'm kind of just going off other posts). Can someone help me out with the logic of this. Thanks for any help. Here is my code:

public class MapExample {

     public static void main(String[] args) {

       Map<String,char[]> mp=new HashMap<String, char[]>();

       char[] words = new char[2];
       words[0] = 'a';
       words[1] = 'b';
       words[2] = 'c';

       mp.put("1", words);
       mp.put("2", words);
       mp.put("3", words);

       Set s=mp.entrySet();
       Iterator it=s.iterator();

        while(it.hasNext())
        {           
            Map.Entry m =(Map.Entry)it.next();
            int key=(Integer)m.getKey();
            String value=(String)m.getValue();

            System.out.println("Key :"+key+"  Value :"+value);
        }
    }
}

5 Answers 5

7

Arrays are zero based. Increase the array size of words so that you can assign an element at index 2

char[] words = new char[3];
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, but now I get java.lang.String cannot be cast to java.lang.Integer as an error, any ideas?
@dave: If you have another question, please post another question instead of discussing in the comments.
@dave your key is a String rather then an Integer, hence the CCE. Use Integer as a key instead
@Reimeus fixed those problems. thanks. Although it produced another type of error - which I guess I'll post in a different question
6
   char[] words = new char[2];
   words[0] = 'a';
   words[1] = 'b';
   words[2] = 'c'; <-- HERE

You create a two elements array but put three items in it.

Arrays in Java have a fixed size which cannot be modified once they are initialized.

And then:

String value=(String)m.getValue();

This won't work: a char[] is not a String!

You have to:

String value = new String((char[]) m.getValue());

But you should you your map's .entrySet(), really.

Comments

3

new char[2] does not mean "make a new array with indexes that go up to 2". It means "make a new array that can hold 2 values", and as array indexes start at 0, the indexes go from 0 to 1.

Comments

1

words[2] is the culprit here! The array words is or size 2. So you can only have Words[0] and words[1]

Comments

1

The integer argument used when we create a new array is not the highest index that should be available in that array but rather the actual size of the array. Therefore, as others have said, you would want

char[] words = new char[3];

to create an array capable of holding three elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.