By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Reply: equals method

P: n/a
**Firstly, apologies for posting this as a new thread. Waiting for
Easynews to turn on posting as the Uni doesn't offer a news server,
and having to post through google.**

Anthony,

Thanks for your suggestion.It certainly helped...

On reading over your suggestion, I tried adding:

for (int Counter = 0; Counter < 50000; Counter++) // Fill array
to prevent Null Errors
adic[Counter].word = new String();

which resolved the original problem, I'd filled the array as a whole,
but forgotten to fill the word part..but it is now throwing a
java.lang.ArrayIndexOutOfBoundsException: 20 with the line

adic[i].links[cnttwo] = adic[cntone].links[0];

which I guess is the second bit you were referring to..

I tried rewriting as:

--

for (int i = 0; i < adic.length; i++) { // Take each member of arrays
dictionary in turn

int cnttwo = 1; // Initialise second count variable, 1 as links[0]
already filled

for (int cntone = 1; cntone < (adic.length - 1); cntone++) { // Take
each member of arrays dictionary in turn
if (adic[i].word.equals(adic[cntone].word)) { // Compare against the
word we're looking for
adic[i].links[cnttwo] = adic[cntone].links[0]; // If it matches,
add it as next reference
//delete adic[cntone] // Delete anagram as repeat, now listed
elsewhere and to prevent multiples
cnttwo++; // Increment reference by one
}
}
}

--

So it should be comparing adic[0].word with adic[1].word, then storing
in adic[0].links[1] if found, before comparing with adic[2].word,
storing in adic[0].links[2] etc... up until the end of the array.
..
I was thinking this would prevent it going over the end of the array,
but didn't have the desired effect... I'm sure I'm missing something
simple..?

Cheers,

Darryl
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Darryl Woodford" <dw****@essex.ac.uk> wrote in message
news:6d**************************@posting.google.c om...

**Firstly, apologies for posting this as a new thread.
Waiting for Easynews to turn on posting as the Uni doesn't
offer a news server, and having to post through google.**

Anthony,

Thanks for your suggestion.It certainly helped...

No worries :) !

On reading over your suggestion, I tried adding:

for (int Counter = 0; Counter < 50000; Counter++)
// Fill array to prevent Null Errors
adic[Counter].word = new String();

which resolved the original problem, I'd filled the array as
a whole, but forgotten to fill the word part.

It's often forgotten, too, that creating an array merely creates a group of
*storage spaces*, and not a group of objects. It is a *very* common
oversight, and should probably be one of the first things [if not the first]
checked when diagnosing array-related problems.

.but it is now throwing a java.lang.ArrayIndexOutOfBoundsException: 20
with the line

adic[i].links[cnttwo] = adic[cntone].links[0];

which I guess is the second bit you were referring to..

My concern was with 'cnttwo' since I noticed it was initialised to a value 1
higher than 'cntone', and *if* it were incremented on *each* iteration it
would likely breach the array bounds if the array were 'tightly sized' [i.e.
created with no safety buffer].

I tried rewriting as:

--

for (int i = 0; i < adic.length; i++) {
// Take each member of arrays dictionary in turn

int cnttwo = 1; // Initialise second count variable, 1 as links[0]
already filled

for (int cntone = 1; cntone < (adic.length - 1); cntone++) { // Take
each member of arrays dictionary in turn
if (adic[i].word.equals(adic[cntone].word)) { // Compare against the
word we're looking for
adic[i].links[cnttwo] = adic[cntone].links[0]; // If it matches,
add it as next reference
//delete adic[cntone] // Delete anagram as repeat, now listed
elsewhere and to prevent multiples
cnttwo++; // Increment reference by one
}
}
}

--

So it should be comparing adic[0].word with adic[1].word,
then storing in adic[0].links[1] if found, before comparing with
adic[2].word, storing in adic[0].links[2] etc... up until the end
of the array.
.
I was thinking this would prevent it going over the end of the array,
but didn't have the desired effect... I'm sure I'm missing something
simple..?


Refering back to this line:

adic[i].links[cnttwo] = adic[cntone].links[0];

you have 3 index values that could [potentially] hold invalid values, and
two arrays that could be sized other than what you may expect ['adic' and
'links' in each 'adic' element]. I would suggest:

* Printing / logging the values of these indexes to identify an
invalid index

* Assuming indexes seem ok, selectively oversizing the arrays
to determine the 'failing' array

I didn't have time to analyse your code, but at a glance believe 'links' and
/ or 'cnntwo' is / are the culprits. Sorry I couldn't give a more definitive
answer.

I hope this helps.

Anthony Borla
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.