Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 09-11-07   #1 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: haslett, MI
Posts: 1,189

Rep: 130 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 95
Trader Rating: 1
Default Ugh... Hashmaps, ArrayLists, Java and me. Any help?

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());
I realize I'm not doing error checking or exception handling, I'm just trying to understand the best way to access the ArrayList returned from the .get function. the underlined portion is where it is erroring.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte EP45-UD3P
Memory
4Gb Gskill 1066Mhz PK's
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb & WD Black 640Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline   Reply With Quote
Old 09-11-07   #2 (permalink)
Clutch ModeliK
 
bigvaL's Avatar
 
intel nvidia

Join Date: Jan 2005
Location: Ontario
Posts: 12,686

FAQs Submitted: 7
Hardware Reviews: 2
Trader Rating: 45
Default

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.

System: Main Gaming Rig
CPU
i7 920 D0
Motherboard
Asus P6T
Memory
6GB GSKILL DDR3-1600
Graphics Card
Sapphire HD5870
Hard Drive
64GB Crucial SSD
Sound Card
X-Fi XtremeMusic
Power Supply
Toughpower 1000watt 108A
Case
Thermaltake Armor Black
CPU cooling
Thermalright Ultra 120 Lapped
GPU cooling
Stock
OS
Windows 7 Pro x64
Monitor
Dell S2209W
Overclock.net - 2009 Chimp Challenge Champions
bigvaL is offline Overclocked Account bigvaL's Gallery   Reply With Quote
Old 09-11-07   #3 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: haslett, MI
Posts: 1,189

Rep: 130 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 95
Trader Rating: 1
Default

Quote:
Originally Posted by bigvaL View Post
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.
I realize the syntax is wrong, I just don't know how to make it right.

Code:
words.get(tempWord)
Returns an object, which I want to be of type ArrayList. Did I do the declaration correctly when I declared the HashMap? If so, doesn't it know that the type is an ArrayList (which is very similar to a vector) and therefore know I can do .add with it? Otherwise, how would I assign the returned object to a variable to be able to add to the vector? when I try:

Code:
				
ArrayList<Integer> a = (words.get(tempWord);
a.add(new Integer(count));
It says "cannot convert from object to ArrayList<Integer>"

Sorry for being nubbish at this, this is my first java coding, I'm more of a C person.Array
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte EP45-UD3P
Memory
4Gb Gskill 1066Mhz PK's
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb & WD Black 640Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"

Last edited by kdbolt70 : 09-11-07 at 08:25 PM
kdbolt70 is offline   Reply With Quote
Old 09-11-07   #4 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: haslett, MI
Posts: 1,189

Rep: 130 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 95
Trader Rating: 1
Default

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);
a is obviously declared as an arraylist<Integer>, and i is obviously an Integer, so why can I not add to a?
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte EP45-UD3P
Memory
4Gb Gskill 1066Mhz PK's
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb & WD Black 640Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline   Reply With Quote
Old 09-11-07   #5 (permalink)
WaterCooler
 
intel nvidia

Join Date: Aug 2005
Posts: 359

Rep: 33 mikester is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default

Quote:
Originally Posted by kdbolt70 View Post
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);
a is obviously declared as an arraylist<Integer>, and i is obviously an Integer, so why can I not add to a?
What do you mean by "it's not letting me add"? What is it doing - compile time error, runtime exception, etc.?

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);
__________________
System: My System
CPU
Q6600 G0 @ 3.6 GHz
Motherboard
Asus P5K (Vanilla) w/ vdroop mod
Memory
6 GB DDR2-800
Graphics Card
eVga 8800GTS
Case
Thermaltake Armor
CPU cooling
DTek Fuzion
GPU cooling
MCW60
OS
Ubuntu 7.10 64 bit
mikester is offline   Reply With Quote
Old 09-11-07   #6 (permalink)
New to Overclock.net
 
intel ati

Join Date: Jun 2007
Posts: 718

Rep: 28 Pasha is acknowledged by some
Unique Rep: 25
Trader Rating: 1
Default

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));
It says "cannot convert from object to ArrayList<Integer>"

you missed a ). it should be
ArrayList<Integer> a = (words.get(tempWord));
__________________
System: lohlz
CPU
C2D e6750 @ 3.5ghz 8 x 438mhz
Motherboard
Abit IP35- E
Memory
2 x 1gb Ballistix 4-4-4-12 @ 876mhz
Graphics Card
Visiontek 4870
Hard Drive
2 x 80GB WD
Sound Card
M-Audio Audiophile
Power Supply
OCZ StealthXStream 600W
Case
Coolermaster CM-690
CPU cooling
AquaXtreame + dB1 + Black Ice Pro
GPU cooling
ZALMAN VF1000
OS
XP pro
Monitor
LG 20inch 5000:1 contrast ratio, 2ms.
Pasha is offline   Reply With Quote
Old 09-11-07   #7 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: haslett, MI
Posts: 1,189

Rep: 130 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 95
Trader Rating: 1
Default

Quote:
Originally Posted by mikester View Post
What do you mean by "it's not letting me add"? What is it doing - compile time error, runtime exception, etc.?

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);
Well, to start off, this initially was a giant error with the fact that I've never used the eclipse editor before. It gives you all these cute little warnings, but they don't go away till you recompile them, even though they appear automatically. This caused me to believe my a.add(i); was still erroring, however that is in fact correct. This is me learning my lessons the hard way.

Next, I'm sorry I didn't post the entirety of the code, I actually do a

Quote:
if(words.containsKey(tempWord)){
before I even do this add. This checks if the word is already in the hashmap. I have an else block if it is a new word added that does what you suggested, add a new arraylist with one element.


Quote:
Originally Posted by Pasha View Post
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));
It says "cannot convert from object to ArrayList<Integer>"

you missed a ). it should be
ArrayList<Integer> a = (words.get(tempWord));

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));
Would be correct if I was mapping the key to a single Integer, but I actually need it mapped to a vector (in this case, ArrayList) of Integers.

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.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte EP45-UD3P
Memory
4Gb Gskill 1066Mhz PK's
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb & WD Black 640Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"

Last edited by kdbolt70 : 09-11-07 at 09:58 PM
kdbolt70 is offline   Reply With Quote
Old 09-12-07   #8 (permalink)
Programmer
 
intel nvidia

Join Date: Jul 2006
Posts: 191

Rep: 12 mjoc13 Unknown
Unique Rep: 11
Trader Rating: 5
Default

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!

System: My System
CPU
C2D e6400
Motherboard
eVga 680i - T1
Memory
2x1GB Gskill HZ
Graphics Card
eVga 8800 gts sc 320
Hard Drive
wd 320 sataII, WD 250 PATA
Sound Card
OB
Power Supply
Corsair HX620W
Case
Coolmaster Elite
OS
Win XP pro sp2
Monitor
Sammy 19 ws LCD

Last edited by mjoc13 : 09-12-07 at 07:55 AM
mjoc13 is offline   Reply With Quote
Old 09-13-07   #9 (permalink)
AMD Overclocker
 
decompiled's Avatar
 
intel ati

Join Date: Feb 2006
Location: Redsox Nation
Posts: 831

Rep: 94 decompiled is acknowledged by some
Unique Rep: 76
Hardware Reviews: 9
Trader Rating: 4
Default

I did a project like this in my Data Structures class a while ago. Hashing is so much fun.

System: HAKR
CPU
T6670
Motherboard
Intel PM45
Memory
4/PQI DDR2
Graphics Card
HD4560
Hard Drive
500/Seagate
Sound Card
Realtek HD Audio
OS
Windows 7 x64
Monitor
16.4
decompiled is offline   Reply With Quote
Old 09-14-07   #10 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: haslett, MI
Posts: 1,189

Rep: 130 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 95
Trader Rating: 1
Default

Quote:
Originally Posted by decompiled View Post
I did a project like this in my Data Structures class a while ago. Hashing is so much fun.
Yeah, the concept of hashing is pretty cool. I've done this type of thing a couple times in C++, but never in Java, which is what is causing me troubles. Nevertheless, I got the project done later in the evening. I'm on vacation now
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte EP45-UD3P
Memory
4Gb Gskill 1066Mhz PK's
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb & WD Black 640Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 05:34 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2010 Shogun Interactive Development. Most rights reserved.
Page generated in 0.23104 seconds with 8 queries