-
July 16th, 2011, 03:53 AM
#1
More Java Help: Splitting an Array
Hi All
this question is from the same class as i have been writing: Basically i have the following method:
public void makeTrainingSchedule(String[] courseNames, Integer[][] badges)
{
Map<String, Integer[]> map1 = new HashMap<String, Integer[]>();
for(int num = 0; num < courseNames.length; num ++)
{
map1.put(courseNames[num], badges[num]);
}
this.trainingRoster = map1;
}
The above takes an array of string and an array of an array of integers and places them in a map. The map is then referenced to an instance variable called trainingRoster.
Now i need to take all the integers out of trainingRoster and place them into a SortedSet as individuals.
I know im probably overthinking this, but how do i go about accessing all the integers in the map of trainingRoster?
Last edited by PhoenixHeights; July 16th, 2011 at 03:56 AM.
-
July 16th, 2011, 04:32 AM
#2
Re: More Java Help: Splitting an Array
Just to add i've tried this
public void uniqueSortedBadges()
{
SortedSet<Integer> sortedBadge = new TreeSet<Integer>();
List<Integer> list1 = new ArrayList<Integer>();
Integer[] cloneBadge;
Set<String> keys = this.trainingRoster.keySet();
for(String item : keys)
{
cloneBadge = this.trainingRoster.get(item);
}
for(Integer item : cloneBadge)
{
}
}
but i just get told that cloneBadge hasnt been initilized
-
July 16th, 2011, 05:30 AM
#3
Re: More Java Help: Splitting an Array
Please use code tags when posting code.
The warning is because you haven't explicitly initialized cloneBadge and it is possible that you will attempt to use it in an un-initialized state ie if trainingRoster is empty the first for-each loop will not be executed and so cloneBadge you not be set to any value. The answer is to either:
1. Set it to null where you declare it and then test it for null after the first for-each loop and only execute the second loop if it is not null.
2. Set it to an empty Integer array where you declare it in which case if the first for loop doesn't execute then the second one wont either.
-
July 16th, 2011, 06:13 AM
#4
Re: More Java Help: Splitting an Array
If trainingRoster is a Map, why get all the keys and use each key to search for its value, when you can just get all the values (using Map.values()) and iterate over them directly (without using the keys)?
To arrive at the simple is difficult...
R. Elisha
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 16th, 2011, 08:15 AM
#5
Re: More Java Help: Splitting an Array
Originally Posted by dlorde
If trainingRoster is a Map, why get all the keys and use each key to search for its value, when you can just get all the values (using Map.values()) and iterate over them directly (without using the keys)?
To arrive at the simple is difficult...
R. Elisha
ive tried doing that but then i get told i couldnt mix Collection and Arrays together.
Im really at a loss how to do this.
Id like to add, i am by no means looking for the answer, as this is a uni assignment, just some direction how to go about it.
i realise the i need to iterate through every element of the map values of trainingRoster, and then add each element to sortedBadge, im just not sure how to go about it.
Last edited by PhoenixHeights; July 16th, 2011 at 08:19 AM.
-
July 16th, 2011, 09:33 AM
#6
Re: More Java Help: Splitting an Array
I've already told you how to get your existing code working.
If you want to change your code to the way dlorde suggested (and if you aren't using the keys in this section of code then his suggestion it is a better way of doing it) post the code you have tried and we will help you.
-
July 16th, 2011, 10:42 AM
#7
Re: More Java Help: Splitting an Array
Originally Posted by PhoenixHeights
public void uniqueSortedBadges()
{
Set<Integer[]> sortedBadge = new TreeSet<Integer[]>();
for(Integer[] item : this.trainingRoster.values())
{
sortedBadge.add(item);
}
System.out.println(String.valueOf(sortedBadge));
}
Clearly it just doesnt work! Like i said im noob and im learning!
-
July 16th, 2011, 10:48 AM
#8
Re: More Java Help: Splitting an Array
Please post code in code tags and not quote tags. And rather than just saying it doesn't work please tell us what doesn't work - is it failing to compile or just not doing what you expect? Finally post any compiler or run time error messages.
-
July 16th, 2011, 10:50 AM
#9
Re: More Java Help: Splitting an Array
Sorry i dont know how to do code tags!
It comiles but i get the following error during run time
Semantic error: [Ljava.lang.Integer; cannot be cast to java.lang.Comparable
-
July 16th, 2011, 10:55 AM
#10
Re: More Java Help: Splitting an Array
Please post the full text of the error message and the line(s) of code that it refers to.
The message seems to say that you are trying to cast an Integer array.
What do you expect/want this expression to do:
String.valueOf(sortedBadge)
What datatype/class is sortedBadge?
What type of argument does the valueOf() method take?
Norm
-
July 16th, 2011, 11:00 AM
#11
Re: More Java Help: Splitting an Array
Originally Posted by Norm
Please post the full text of the error message and the line(s) of code that it refers to.
The message seems to say that you are trying to cast an Integer array.
What do you expect/want this expression to do:
String.valueOf(sortedBadge)
What datatype/class is sortedBadge?
What type of argument does the valueOf() method take?
Right, i want the method to take all the values from the Map instance variable trainingRoster, and place them into a Set as indiviual elements. The Values are an array of an array tho.
Im afraid that is the full error message.
If im honest im not sure about: String.valueOf(sortedBadge), i thought everything had to be converted to a string before its printed.
-
July 16th, 2011, 11:02 AM
#12
Re: More Java Help: Splitting an Array
Sorry i dont know how to do code tags!
Read the bottom of each of my posts - it explains how to do it.
The problem is because you are using a sorted set and items added to a sorted set must be Comparable. Integer arrays are not, but you can provide your own Comparator object when you construct the TreeSet.
-
July 16th, 2011, 11:07 AM
#13
Re: More Java Help: Splitting an Array
i thought everything had to be converted to a string before its printed
sortedBadge is not an primitive, its a class. The String valueOf() method probably does not know what to do with it. You may have to extract its contents item by item and print them individually.
What line of code causes the error?
Norm
-
July 16th, 2011, 11:08 AM
#14
Re: More Java Help: Splitting an Array
Originally Posted by keang
Read the bottom of each of my posts - it explains how to do it.
The problem is because you are using a sorted set and items added to a sorted set must be Comparable. Integer arrays are not, but you can provide your own Comparator object when you construct the TreeSet.
Sorry i dont see any signature at the bottom of your posts, i'll try to write a
I'll write a new compareTo method that allows Integer arrays and let you know the results.
-
July 16th, 2011, 11:17 AM
#15
Re: More Java Help: Splitting an Array
Sorry, this is starting to get messy! Lets start again!
I have the following code:
Code:
public void makeTrainingSchedule(String[] courseNames, Integer[][] badges)
{
Map<String, Integer[]> map1 = new HashMap<String, Integer[]>();
for(int num = 0; num < courseNames.length; num ++)
{
map1.put(courseNames[num], badges[num]);
}
this.trainingRoster = map1;
}
This takes a string array and and integer array and places them into a map. so the result is something like this:
two : [10, 20, 30, 40]
one : [1, 2, 3]
three : [200, 400, 100, 300, 500]
Now i want a new method to take all those integers and place them into a Sorted Set! The problem im having is iterating over the Map trainingRoster' values and placing every individual element into a sorted set. well it doesnt have to be a sorted set i just need to be able to arrange the numbers in order.
Last edited by PhoenixHeights; July 16th, 2011 at 11:21 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|