|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Ugh... Hashmaps, ArrayLists, Java and me. Any help?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Programmer
![]() |
So I'm working on this program where I'm hashing a word and have an ArrayList of Integers (not ints, as ArrayList only works with objects) as the value. I'm trying to reference the ArrayList and add a new entry to it if the word is in the hashmap, otherwise add a new ArrayList to the value of the hashmap. It is not letting me do a .add to the returned ArrayList object if I do find a match in the hashmap. Here's code:
Code:
HashMap<String, ArrayList<Integer>> words = new HashMap<String, ArrayList<Integer>>();
int count = 0;
String line = "";
String tempWord = "";
File srcFile = new File(file);
BufferedReader in = new BufferedReader(new FileReader(srcFile));
do {
line = in.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
tempWord = tokenizer.nextToken();
if(words.containsKey(tempWord)){
words.get(tempWord).add(new Integer(count));
}
else{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(new Integer(count));
words.put(tempWord, list);
}
count++;
}while (in.ready());
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Clutch ModeliK
![]() |
words.get(tempWord).add(new Integer(count));
That's incorrect syntax I believe.. Look it up on javadocs and it'll give you the correct syntax.
__________________
FOLD FOR OCN - TEAM 37726 CS:S Server - GunGame 100tick Ping Boosted 32 Slot: overclock.nuclearfallout.net:27015 OC Settings: 3800mhz 181x21, 1.275vcore, 1.65vdimm.
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Programmer
![]() |
Quote:
Code:
words.get(tempWord) Code:
ArrayList<Integer> a = (words.get(tempWord); a.add(new Integer(count)); Sorry for being nubbish at this, this is my first java coding, I'm more of a C person.Array
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 09-11-07 at 08:25 PM |
||||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Programmer
![]() |
Ok, I've gotten it down to this, and I don't know why its not letting me do a.add(i);
Code:
ArrayList<Integer> a = (ArrayList<Integer>) words.get(tempWord); Integer i = new Integer(count); a.add(i); words.put(tempWord, a);
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||
|
WaterCooler
![]() |
Quote:
Just looking at this code, I can see one glaring error - you're not handling the possibility of "tempWord" not being in the hashmap to start with. In that case, it will return "null". You should handle this case by creating a new empty array list and then adding "i", like this: Code:
ArrayList<Integer> a = (ArrayList<Integer>) words.get(tempWord);
if (a == null)
a = new ArrayList<Integer>();
Integer i = new Integer(count);
a.add(i);
words.put(tempWord, a);
|
||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
New to Overclock.net
![]() |
I don't get your code.
__________________Your calling for the object in the arraylist with get method. Then you're adding with the add method to the object that was just got. words.get(tempWord).add(new Integer(count)); Shouldn't you just add to the ArrayList like so, words.add(new Integer(count)); also here Code:
ArrayList<Integer> a = (words.get(tempWord); a.add(new Integer(count)); you missed a ). it should be ArrayList<Integer> a = (words.get(tempWord));
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||||
|
Programmer
![]() |
Quote:
Next, I'm sorry I didn't post the entirety of the code, I actually do a Quote:
Quote:
I think you're misunderstanding my code. Its kind of complicated, but we're going for efficiency, which is why I'm using a hashmap that maps each key to a dynamic ArrayList. The line Code:
words.add(new Integer(count)); Edit: I think I see what you think is happening. the object words is not an ArrayList, it is actually the name of the hashmap. so a word.get() returns an object of type ArrayList.
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 09-11-07 at 09:58 PM |
||||||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||
|
Programmer
|
Edit: NM i think you got it
A side note you can turn the auto "compilation on save" on, so it recompiles every time you save a class. Go "Project->Build Automatically"
__________________
Relax and enjoy life!
Last edited by mjoc13 : 09-12-07 at 07:55 AM |
|||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||||
|
Programmer
![]() |
Quote:
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|