You are on page 1of 9

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

This week's book giveaway is in the HTML and JavaScript forum. We're giving away four copies of Early Release edition of Programming JavaScript Applications and have Eric Elliott on-line! See this thread for details.

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

Win a copy of Early Release Edition of Programming JavaScript Applications this week in the HTML and JavaScript forum!

JavaRanch Java Forums Java Java in General

Author

Help with Data Array for Hash Table creation


posted 7/18/2010 6:18:38 PM

Joanna Spence Greenhorn Joined: May 31, 2010 Posts: 23

Hello, I am working with building Hash Tables, with Linear Probing and I have three class files. The first is a driver file that calls the super class HashTable. It generates random values to fit the size of the hash table indicated in the driver file Here is the code snippet from the driver file:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 .

i n tn u m=5 ; i n t[ ]s i z e s={ 1 0 0 9 ,2 0 0 3 ,4 0 0 1 ,8 0 0 9 ,1 6 0 0 1 } ; i n t[ ]d h s={ 9 9 7 ,1 9 9 9 ,3 9 8 9 ,7 9 9 3 ,1 5 9 9 1 } ; d o u b l e[ ]a l p h a s={ 0 . 5 ,0 . 6 ,0 . 7 ,0 . 8 ,0 . 9 ,0 . 9 5 } ; H T a b l eL P T ,D H T ,S C T ; f o r( i n ti=0 ;i<s i z e s . l e n g t h ;i + + ) { i n ts i z e=s i z e s [ i ] ; i n td h m o d=d h s [ i ] ; L P T=n e wL P T a b l e ( s i z e ) ; / /N o t ec o n s t r u c t o r sf o rc l a s s e sb e l o w . D H T=n e wD H T a b l e ( s i z e ,d h m o d ) ; / /A l s on o t et h a tA L Lv a r i a b l e sa r eo f S C T=n e wS C T a b l e ( s i z e ) ; / /c l a s sH T a b l e f o r( i n tj=0 ;j<a l p h a s . l e n g t h ;j + + ) { L P T . i n i t ( ) ; / /I n i tm e t h o dc a l l e de a c ht i m et h r o u g ht h i sl o o pt or e s e t D H T . i n i t ( ) ; / /t h eh a s ht a b l e st oe m p t y
1/9

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . v a l u e s 3 0 . 3 1 . 3 2 . 3 3 . 3 4 .

S C T . i n i t ( ) ; d o u b l ea l p h a=a l p h a s [ j ] ; / /G e tn e x ta l p h av a l u e i n tn=( i n t )( s i z e*a l p h a ) ; / /H o wm a n yi t e m st oi n s e r t i n tk e y ; f o r( i n tk=0 ;k<n ;k + + ) { k e y=( i n t )( M a t h . r a n d o m ( )*( 4*s i z e ) ) ; / /M a k er a n g eo f / /4t i m e st h es i z eo ft h et a b l e L P T . i n s e r t ( k e y ) ; D H T . i n s e r t ( k e y ) ; / /i n s e r tk e yi n t oe a c ht a b l e S C T . i n s e r t ( k e y ) ; }

It first creates a new object called HTable which is the super class in my other class files... and then generate random values denoted by n of how many items to insert. It calls my "insert" method in my subclass for Linear Probing... My problem is, in my subclass for LPTable... I need to generate the array of values of the number of items to insert, but when I create the data array, it is only inserting one value... I believe it is my placement of the creation of the data array in my subclass... Here is my subclass and what I have... I need to generate the complete data array of all the values so that I can perform the hash table functions, but I also want to print out the data array to test to see they are all there... Here is my SuperClass code snippet:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 .

/ /M e t h o dt oi n i t i a l i z et h et a b l ei na n yw a yn e c e s s a r y p u b l i ca b s t r a c tv o i di n i t ( ) ; / /I n s e r ta ni n ti n t ot h eh a s ht a b l e p u b l i ca b s t r a c tv o i di n s e r t ( i n tk e y ) ;

Here is my subclass code:

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 .

p u b l i ci n t [ ]l p T a b l e A r r a y ; p u b l i cA r r a y L i s t < I n t e g e r >l p T a b l e L i s t ; p u b l i cv o i di n s e r t ( i n tk e y ) { i n ta d d i t i o n ; a d d i t i o n = k e y ; i n th a s h V a l = h ( k e y ) ; L P T . p u t ( a d d i t i o n ,h a s h V a l ) ; S y s t e m . o u t . p r i n t l n ( L P T ) ;

}
2/9

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

When I run my program right now, it only inserts one value...i need to insert all random values in ONE call, and then I want to print the result to see that it performed the operation correcltly. Any help would be appreciated..I think it my placement of the declarations of the array and the call to insert but I'm not sure.

David Newton Author Rancher Joined: Sep 29, 2008 Posts: 12617
I like...

posted 7/18/2010 7:07:19 PM

Well, if you think it's in the creation of your data array, it'd be good to show the creation of the data array. The code fragment has too many (to us) unknowns (edited to remove the unnecessary noise):
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 .

p u b l i ci n t [ ]l p T a b l e A r r a y ;/ /C r e a t e dw h e r e ? p u b l i cA r r a y L i s t < I n t e g e r >l p T a b l e L i s t ;/ /C r e a t e dw h e r e ? p u b l i cv o i di n s e r t ( i n tk e y ) { L P T . p u t ( k e y ,h ( k e y ) ) ;/ /W h a t ' sh ?W h a t ' sL P T ? }

Joanna Spence Greenhorn Joined: May 31, 2010 Posts: 23

posted 7/18/2010 8:12:56 PM

The lpTableArray is created in the LPT subClass... lpTableList is also created in the subClass LPT... the "h" stands for the hash... In the driver class; a new instance of LPT is created, and the inserts are done from the driver...

David Newton Author Rancher Joined: Sep 29, 2008 Posts: 12617
I like...

posted 7/18/2010 8:23:47 PM

*confused* But LPT is a local variable in the driver--how does a subclass have access to it? That aside--I don't see how to help; unless your loop isn't executing as many times as you think it is, or the key you're adding is always the same, from the code given, there's no reason it would be only "inserting once". So there's some other logical error not contained in the code provided. Since we can't run it, there's nothing we can do except guess.

Joanna Spence Greenhorn Joined: May 31, 2010 Posts: 23

posted 7/19/2010 4:40:27 AM

Here are the files, I tried to attach them but it wouldn't let me attach in .java or .txt format... Here is the driver:

view plain

c opy to c lipboard

print

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

3/9

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 .

i m p o r tj a v a . i o . * ; p u b l i cc l a s sd r i v e r { p u b l i cs t a t i cv o i dm a i n ( S t r i n g[ ]a r g s ) { P r i n t W r i t e ro f i l e=n u l l ; t r y{ o f i l e=n e wP r i n t W r i t e r ( n e wF i l e O u t p u t S t r e a m ( " a 2 o u t . t x t " ) ) ; } c a t c h( F i l e N o t F o u n d E x c e p t i o ne ) { S y s t e m . o u t . p r i n t l n ( " O u t p u tF i l eD N E " ) ; } i n tn u m=5 ; i n t[ ]s i z e s={ 1 0 0 9 ,2 0 0 3 ,4 0 0 1 ,8 0 0 9 ,1 6 0 0 1 } ; i n t[ ]d h s={ 9 9 7 ,1 9 9 9 ,3 9 8 9 ,7 9 9 3 ,1 5 9 9 1 } ; d o u b l e[ ]a l p h a s={ 0 . 5 ,0 . 6 ,0 . 7 ,0 . 8 ,0 . 9 ,0 . 9 5 } ; H T a b l eL P T ,D H T ,S C T ; f o r( i n ti=0 ;i<s i z e s . l e n g t h ;i + + ) { i n ts i z e=s i z e s [ i ] ; i n td h m o d=d h s [ i ] ; L P T=n e wL P T a b l e ( s i z e ) ; / /N o t ec o n s t r u c t o r sf o rc l a s s e sb e l o w . D H T=n e wD H T a b l e ( s i z e ,d h m o d ) ; / /A l s on o t et h a tA L Lv a r i a b l e sa r eo f S C T=n e wS C T a b l e ( s i z e ) ; / /c l a s sH T a b l e f o r( i n tj=0 ;j<a l p h a s . l e n g t h ;j + + ) { L P T . i n i t ( ) ; / /I n i tm e t h o dc a l l e de a c ht i m et h r o u g ht h i sl o o pt or e s e t D H T . i n i t ( ) ; / /t h eh a s ht a b l e st oe m p t y S C T . i n i t ( ) ; d o u b l ea l p h a=a l p h a s [ j ] ; / /G e tn e x ta l p h av a l u e i n tn=( i n t )( s i z e*a l p h a ) ; / /H o wm a n yi t e m st oi n s e r t i n tk e y ; f o r( i n tk=0 ;k<n ;k + + ) { k e y=( i n t )( M a t h . r a n d o m ( )*( 4*s i z e ) ) ; / /M a k er a n g eo f v a l u e s / /4t i m e st h es i z eo ft h et a b l e L P T . i n s e r t ( k e y ) ; D H T . i n s e r t ( k e y ) ; / /i n s e r tk e yi n t oe a c ht a b l e S C T . i n s e r t ( k e y ) ; } i n tL P F o u n d=0 ,L P F o u n d P r o b e s=0 ; i n tL P N o t=0 ,L P N o t P r o b e s=0 ; i n tD H F o u n d=0 ,D H F o u n d P r o b e s=0 ; i n tD H N o t=0 ,D H N o t P r o b e s=0 ; i n tS C F o u n d=0 ,S C F o u n d P r o b e s=0 ; i n tS C N o t=0 ,S C N o t P r o b e s=0 ; f i n a li n tF i n d R u n s=4 0 0 0 0 ; / /D o4 0 0 0 0s e a r c h e sf o re a c hs e t u p f o r( i n ti i=0 ;i i<F i n d R u n s ;i i + + ) { k e y=( i n t )( M a t h . r a n d o m ( )*( 4*s i z e ) ) ; b o o l e a nf o u n d 1=L P T . f i n d ( k e y ) ; / /T h ef i n dm e t h o ds h o u l d / /i n i t i a l i z ea" p r o b "i n s t a n c ev a r i a b l et o0 ,t h e n / /i n c r e m e n ti tf o re a c hp r o b en e e d e dd u r i n gt h ef i n d .

4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 .
www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

4/9

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . P N o t ) ; 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . H N o t ) ; 1 2 3 . 1 2 4 . 1 2 5 . 1 2 6 . 1 2 7 . C N o t ) ; 1 2 8 . 1 2 9 . 1 3 0 . " ) ; 1 3 1 . 1 3 2 . e s s ) ; 1 3 3 . P F o u n d ) ; 1 3 4 . P N o t ) ; 1 3 5 . ) ;

i f( f o u n d 1= =t r u e ) { L P F o u n d + + ; L P F o u n d P r o b e s+ =L P T . p r o b e s ( ) ; / /T h ep r o b e s ( )m e t h o d / /r e t r i e v e st h ev a l u eo b t a i n e db yt h e" p r o b e c o u t " / /v a r i a b l ed u r i n gt h ef i n d } e l s e { L P N o t + + ; L P N o t P r o b e s+ =L P T . p r o b e s ( ) ; } b o o l e a nf o u n d 2=D H T . f i n d ( k e y ) ; i f( f o u n d 2= =t r u e ) { D H F o u n d + + ; D H F o u n d P r o b e s+ =D H T . p r o b e s ( ) ; } e l s e { D H N o t + + ; D H N o t P r o b e s+ =D H T . p r o b e s ( ) ; } b o o l e a nf o u n d 3=S C T . f i n d ( k e y ) ; i f( f o u n d 3= =t r u e ) { S C F o u n d + + ; S C F o u n d P r o b e s+ =S C T . p r o b e s ( ) ; } e l s e { S C N o t + + ; S C N o t P r o b e s+ =S C T . p r o b e s ( ) ; } } / /B e l o wIa mc a l c u l a t i n gt h ev a r i o u sa v e r a g e sf o rt h es c h e m e s d o u b l eA v e L P F o u n d ,A v e L P N o t ,A v e L P T o t a l ,L P S u c c e s s ; d o u b l eA v e D H F o u n d ,A v e D H N o t ,A v e D H T o t a l ,D H S u c c e s s ; d o u b l eA v e S C F o u n d ,A v e S C N o t ,A v e S C T o t a l ,S C S u c c e s s ;

A v e L P F o u n d=( d o u b l e )L P F o u n d P r o b e s/L P F o u n d ; A v e L P N o t=( d o u b l e )L P N o t P r o b e s/L P N o t ; A v e L P T o t a l=( d o u b l e )( L P F o u n d P r o b e s+L P N o t P r o b e s ) / ( L P F o u n d+L L P S u c c e s s=( d o u b l e )L P F o u n d/ ( L P F o u n d+L P N o t ) ; A v e D H F o u n d=( d o u b l e )D H F o u n d P r o b e s/D H F o u n d ; A v e D H N o t=( d o u b l e )D H N o t P r o b e s/D H N o t ; A v e D H T o t a l=( d o u b l e )( D H F o u n d P r o b e s+D H N o t P r o b e s ) / ( D H F o u n d+D D H S u c c e s s=( d o u b l e )D H F o u n d/ ( D H F o u n d+D H N o t ) ; A v e S C F o u n d=( d o u b l e )S C F o u n d P r o b e s/S C F o u n d ; A v e S C N o t=( d o u b l e )S C N o t P r o b e s/S C N o t ; A v e S C T o t a l=( d o u b l e )( S C F o u n d P r o b e s+S C N o t P r o b e s ) / ( S C F o u n d+S S C S u c c e s s=( d o u b l e )S C F o u n d/ ( S C F o u n d+S C N o t ) ; o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " T a b l es i z e :"+s i z e+" A l p h a :"+a l p h a ) ; o f i l e . p r i n t l n ( " L Pf r a c t i o no fs u c c e s s f u lf i n d s :"+L P S u c c o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " L PA v ep r o b e sf o rs u c c e s s f u lf i n d :"+A v e L L PA v ep r o b e sf o ru n s u c c e s s .f i n d :"+A v e L L PA v ep r o b e sf o ra l lf i n d s :"+A v e L P T o t a l
5/9

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

1 3 6 . 1 3 7 . 1 3 8 . e s s ) ; 1 3 9 . H F o u n d ) ; 1 4 0 . H N o t ) ; 1 4 1 . ) ; 1 4 2 . 1 4 3 . 1 4 4 . e s s ) ; 1 4 5 . C F o u n d ) ; 1 4 6 . C N o t ) ; 1 4 7 . ) ; 1 4 8 . 1 4 9 . 1 5 0 . 1 5 1 . 1 5 2 . 1 5 3 .

o f i l e . p r i n t l n ( " T a b l es i z e :"+s i z e+" A l p h a :"+a l p h a ) ; o f i l e . p r i n t l n ( " D Hf r a c t i o no fs u c c e s s f u lf i n d s :"+D H S u c c o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " D HA v ep r o b e sf o rs u c c e s s f u lf i n d :"+A v e D D HA v ep r o b e sf o ru n s u c c e s s .f i n d :"+A v e D D HA v ep r o b e sf o ra l lf i n d s :"+A v e D H T o t a l

o f i l e . p r i n t l n ( " T a b l es i z e :"+s i z e+" A l p h a :"+a l p h a ) ; o f i l e . p r i n t l n ( " S Cf r a c t i o no fs u c c e s s f u lf i n d s :"+S C S u c c o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " o f i l e . p r i n t l n ( " S CA v ep r o b e sf o rs u c c e s s f u lf i n d :"+A v e S S CA v ep r o b e sf o ru n s u c c e s s .f i n d :"+A v e S S CA v ep r o b e sf o ra l lf i n d s :"+A v e S C T o t a l

} / /e n di n n e rf o rl o o p }/ /e n do u t e rf o rl o o p o f i l e . c l o s e ( ) ; } }

Here is the superclass:


view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 .

p u b l i ca b s t r a c tc l a s sH T a b l e { p r o t e c t e di n ts i z e ; / /S i z eo fh a s ht a b l ea r r a y p r o t e c t e di n tp r o b ; / /n u m b e ro fp r o b e sr e q u i r e d p u b l i cH T a b l e ( i n ts z ) { s i z e=s z ; p r o b=0 ; } / /B a s ec o n s t r u c t o r-n o t et h a ty o u / /w i l la l s on e e dac o n s t r u c t o rf o r / /e a c ho fy o u rs u b c l a s s e s

/ /M e t h o dt oi n i t i a l i z et h et a b l ei na n yw a yn e c e s s a r y p u b l i ca b s t r a c tv o i di n i t ( ) ; / /S i m p l eh a s hf u n c t i o n . T h i si si n h e r i t e db yt h es u b c l a s s e sa n d / /s h o u l db eu s e di ne a c ho ft h e m . p u b l i ci n th ( i n tk e y ) { r e t u r n( k e y%s i z e ) ; } / /I n s e r ta ni n ti n t ot h eh a s ht a b l e p u b l i ca b s t r a c tv o i di n s e r t ( i n tk e y ) ; / /R e t u r nt r u ei ft h ek e yi sf o u n d ;f a l s eo t h e r w i s e . D u r i n gt h ef i n d / /t h ep r o bi n s t a n c ev a r i a b l es h o u l di n i t i a l i z e dt o0a n dt h e nu p d a t e d / /a c c o r d i n g l y . p u b l i ca b s t r a c tb o o l e a nf i n d ( i n tk e y ) ; / /R e t u r nt h ec u r r e n tv a l u eo ft h ep r o bv a r i a b l e p u b l i ci n tp r o b e s ( ) { r e t u r np r o b ; } }

Here is the subclass for LPTable:


view plain c opy to c lipboard print ?

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

6/9

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 .

i m p o r tj a v a . u t i l . * ; i m p o r tj a v a . a w t . * ; i m p o r tj a v a . i o . I O E x c e p t i o n ; p u b l i cc l a s sL P T a b l ee x t e n d sH T a b l e { p u b l i cH a s h t a b l eL P T = n e wH a s h t a b l e ( ) ; p u b l i ci n t [ ]s i z e s ; p r i v a t ei n ta r r a y S i z e ; p r i v a t ei n tp r o b e C o u n t ; L P T a b l e ( i n ts ) { s u p e r ( s ) ; a r r a y S i z e = s ; p r o b e C o u n t = 0 ;

/ / S y s t e m . o u t . p r i n t l n ( s i z e ) ; } p u b l i ci n th ( i n tk e y ) { r e t u r nk e y%a r r a y S i z e ; } p u b l i cb o o l e a nf i n d ( i n tn ) { b o o l e a nf o u n d = f a l s e ; i n tl p K e y = n ; r e t u r nf o u n d ; } p u b l i ci n t [ ]l p T a b l e A r r a y ; p u b l i cA r r a y L i s t < I n t e g e r >l p T a b l e L i s t ; p u b l i cv o i di n s e r t ( i n tk e y ) { i n ta d d i t i o n ; a d d i t i o n = k e y ; i n th a s h V a l = h ( k e y ) ; L P T . p u t ( a d d i t i o n ,h a s h V a l ) ; S y s t e m . o u t . p r i n t l n ( L P T ) ;

} p u b l i cA r r a y L i s t < I n t e g e r >l p H a s h 2 ; p u b l i cv o i di n s e r t 2 ( A r r a y L i s t < I n t e g e r >i t e m ) { l p H a s h 2 = n e wA r r a y L i s t < I n t e g e r > ( i t e m ) ; i n th a s h I t e m = l p H a s h 2 . g e t ( 0 ) ; i n th a s h V a l = h ( h a s h I t e m ) ; S y s t e m . o u t . p r i n t l n ( h a s h V a l ) ; w h i l e ( l p H a s h 2 . g e t ( h a s h V a l ) ! = n u l l ) { + + h a s h V a l ; h a s h V a l% =a r r a y S i z e ; } } p u b l i ci n tp r o b e s ( ) { r e t u r np r o b ;


7/9

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 .

} p u b l i cv o i di n i t ( ) { L P T . c l e a r ( ) ; }

David Newton Author Rancher Joined: Sep 29, 2008 Posts: 12617
I like...

posted 7/19/2010 5:50:33 AM

A warning that it prints the entire hash for every single insert would have been in order :( Not cool. Now: what makes you think there's only a single insert happening, which the deluge of S.o.p's gives lie to? And what is the point of the following method, apparently used to determine if something is found in the hash table?
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

p u b l i cc l a s sL P T a b l ee x t e n d sH T a b l e { p u b l i cH a s h t a b l eL P T=n e wH a s h t a b l e ( ) ; p u b l i cb o o l e a nf i n d ( i n tn ){ b o o l e a nf o u n d = f a l s e ; i n tl p K e y = n ; r e t u r nf o u n d ; } }

Will this ever indicate that a key is found? Some notes: - Pick spaces, or tabs, but not both. - Remove code that isn't used. - Remove code that *is* used, but is superfluous. - Remove excess blank lines. - Remove comments that do not add to understanding. - Start with much, much smaller values until you actually know it works, otherwise debug runs take too long.

David Newton Author Rancher Joined: Sep 29, 2008 Posts: 12617
I like...

posted 7/19/2010 8:33:10 AM

- Why does the subclass have its own probe count? But return the superclass's prob count? - Why does LPT re-implement h() with the same implementation? - What's the difference between LPT's arraySize and HTable's size? - Why does LPT.insert2() create a new array list? Named hash? Of the same size as the item you're inserting? - Why does the program attempt to write to ofile even if there's an exception? - Why is the average/etc. shown for every findrun rather than at the end of all of them? (Also, Java conventions state that classes should begin with a capital letter, and non-final variables begin with lower-case letters. Following the conventions makes your code easier to read for other Java programmers :)

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

8/9

5/10/13

Help with Data Array for Hash Table creation (Java in General forum at JavaRanch)

LPTable seems to be a mish-mash of nonsensical code; what specifically are you trying to do? You might want to consider starting over, taking care to keep only the code you actually need and know to work correctly.

I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.

subject: Help with Data Array for Hash Table creation

Similar Threads storing a set of integers in an array hashing problem OutOfMemoryError - how do I find out where the memory is going? Basic Java - Please help a stressed noob with a method How to find repeated value in array?
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 09, 2013 08:55:08 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/503247/java/java/Data-Array-Hash-Table-creation#2269733

9/9

You might also like