Re: Reply: equals method
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/18/03
- Next message: Jesper Matthiesen: "AWT vs. Swing in applets?"
- Previous message: Darryl Woodford: "Reply: equals method"
- In reply to: Darryl Woodford: "Reply: equals method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 18 Nov 2003 03:04:26 GMT
"Darryl Woodford" <dwoodf@essex.ac.uk> wrote in message
news:6d51bd21.0311171633.3e100834@posting.google.com...
>
> **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
- Next message: Jesper Matthiesen: "AWT vs. Swing in applets?"
- Previous message: Darryl Woodford: "Reply: equals method"
- In reply to: Darryl Woodford: "Reply: equals method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|