• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java question... making a Map into an Array

S

SpeedRunner

Guest
For anyone filmiliar with the Map interface, I need to change my Map into an Array.

Student[] stuArray = stuMap.toArray(new Student[stuMap.size()]);

That's what it would be, if only I had a toArray method. There has got to be a way to make it into an array indirectly, anyone know how?
 
Ok, related question.

Student[] stuArray = stuMap.entrySet().toArray(new Student[stuMap.size()]);

The entrySet method converts the Map into the Set, and the Set interface has a toArray method which is what I need. However, I believe this line of code is still wrong. I'm getting a runtime error. The question I want to know is, not just what is wrong, but WHY is it wrong? Thats what I can't figure out.
 
In your second post the problem is that toArray() does not take a parameter. It should look like this:

Student[] stuArray = stuMap.entrySet().toArray();
 
It does take a parameter. It won't compile if I take that out. :confused:

I posted that line of code because that's where I believe the problem lies. But here's the rest of the method.

Code:
    public String numberString()
    {
       Student[] stuArray =  stuMap.entrySet().toArray(new Student[stuMap.size()]);
       Arrays.sort(stuArray, new Student.NumberComparator());
       String stuString = "";
       for(int i = 0; i < stuArray.length; i++)
       {
          stuString += stuArray[i] + "\n";
       }
       return stuString;
    }
 
I'm not quite sure what you're trying to do. A map is essentially two related arrays: an array of keys and an array of values. Are you wanting to get the keys or the values?
 
generelz said:
I'm not quite sure what you're trying to do. A map is essentially two related arrays: an array of keys and an array of values. Are you wanting to get the keys or the values?

values.
 
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html#values()

I will assume you know how to convert a collection to an array as you already did that above.
 
BTW SpeedRunner now that you're going to be coding a lot of Java, you should really use the Java API docs as your first line of defense. If you had read the documentationf for HashMap#entrySet you would have read: "Each element in the returned collection is a Map.Entry.".

Your problem is that you were trying to convert a set of Map.Entry to an array of Students. The runtime error you were receiving was more than likely a ClassCastException when the java code behind Set#toArray was trying to cast a Map.Entry as a Student object.

This particular issue was addressed in the latest release of java (1.5 or just version 5) by adding generics which among other things allow the compiler to do compile-time type checking. If you were using 1.5 the compiler would have complained about incompatible types when using the toArray method.
 
generelz said:
BTW SpeedRunner now that you're going to be coding a lot of Java, you should really use the Java API docs as your first line of defense. If you had read the documentationf for HashMap#entrySet you would have read: "Each element in the returned collection is a Map.Entry.".

Your problem is that you were trying to convert a set of Map.Entry to an array of Students. The runtime error you were receiving was more than likely a ClassCastException when the java code behind Set#toArray was trying to cast a Map.Entry as a Student object.

This particular issue was addressed in the latest release of java (1.5 or just version 5) by adding generics which among other things allow the compiler to do compile-time type checking. If you were using 1.5 the compiler would have complained about incompatible types when using the toArray method.

Thank you so much for the explanation. I was really interested on the reason why.
 
Back
Top