• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there an array class which resequences itself?

 
Dan Kempten
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are awful

After dealing with arrays like this : XyArray myXyArray = new XyArray[20];

and all that kind of stuff I've stepped up into : List<Xy> myXyArray = new ArrayList();

Still I find that when my List<Xy> array has three objects, with name attributes "One", "Two", "Three"
and I delete the object "Two", then the List<Xy> array *still* thinks that it's size is 3

Example : I do this :


and it thinks that when i = 0 the object is "One"
when i = 1 the object is "Three"
when i = 2 !!! hey !!! why is size() still 3 ???

Question : must I extend List / ArrayList to add the concept of reducing size after removing???
Or is there an Array class with this included already in existence???

Thanks for any tips.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is xyOne, xyTwo and xyThree?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I didn't notice this before... Is it a typo or do you remove xyTwo from the wrong list? You are adding elements to "myXyArray" but you are trying to remove an element from "xyArray". Maybe that explains the unexpected behavior?

Marco
 
Dan Kempten
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xy xyOne;
Xy xyTwo;
Xy xyThree;

This problem has been resolved but I am not sure how.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you should post all the relevant parts of your code if you're expecting to get answers here. Nobody knows how the class Xy looks like.
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Kempten wrote:This problem has been resolved but I am not sure how.

Us neither, because you haven't provided us with what you ran.

1. It doesn't compile.
2. Marco already pointed you to your typo. If that is, in fact, what you ran, then nothing would have been removed from myXyArray.

The fact is: size() does update after a successful remove(), except (possibly) in multi-threaded situations; so plainly you were doing something else wrong.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic