• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Really strange behavior in an ArrayList

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making a small program for one of my CS classes that basically demonstrates mastery of java and programming and such. I have been programming for slightly over a decade, so I assumed that as long as I read the manual I would do fine. This program lets the user create shapes on a board, drag them around, and resize them. I have all of those operations working except for a small bug in the dragging that I cannot seem to work out.

I use an arraylist to store all my shapes that are currently being displayed. At all times the shapes have to be depth sorted with the lowest y values being at the back. To do this I created a Comparable which does a comparison on the shape's position. I then sort the Arraylist using this every time I make a change to the positions of the shapes. However, if I click on a shape with a high y value and move it up, it then acts like a kind of pushbar and pushes all shapes that match it's y at any given time to keep going with it. I don't understand how this can happen. My code can only support modifying one object at once, but it seems to be modifying all objects that don't match a certain condition. Can anyone explain this to me?

EDIT: I just commented out the sort lines and it works properly. That points to the sorter as the culprit. Can a sorter place two items "next" to eachother in a 1D array like an ArrayList? That doesn't exactly seem intuitive.

My MouseAdapter and Comparator (they are inside a class which contains the ArrayList shapes along with the booleans fill and mode:
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kevin and welcome to Javaranch!

A couple of random hints

1.) What does the Javadoc for ArrayList say about maintaining order?
2.) Have you printed out the contents of your ArrayList before/after you sort? What happens?

Let us know how you get on!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic