Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting an reference to an object rather than a value  RSS feed

 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class Airport that contains 3 attributes:
- name and code are String
- destinations is an ArrayList of Airport

What I want to achieve is the following: rather than the list of Airport containing values, it should contain references. Why? Because I could then have a scenario as follows:

The point is that in this way I can start from an Airport and navigate from one given airport to another (regardless of the depth) as in a graph.

Any ideas how to proceed?

Thanks guys,
David.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that you want to use an arraylist when you are only using the first item? This looks to me more like a linked list which you could code by adding an airport variable like so:

If I'm wrong and you have to use an ArrayList, then if you use a generic ArrayList<Airport>, your calls to .get(0) would work. If you use a non-generic ArrayList, you can still get it to work, but you'd have to do a bunch of casts.
[ April 06, 2007: Message edited by: pete stein ]
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your input but no it's not what I'm after... What you describe is roughly what I've got. And I use arraylists only because an airport can have more than one destination. The get(0) was just a sample in my code.

I do really need to have references to my airports in my arraylist no the actual values. Why? Because imagine this:

This would print out "Buffalo and Buffalo don't have as many destinations".

The scenario I imagined isn't far-fetched at all. Imagine I have some data representing airports, their names, their codes. I'm parsing that data to build Airport objects.

A holds destination B but has no knowledge of the latter's destinations as they are added later on to b. If I used references rather than values, it would work though.

In general how can I force Java to make a copy by reference rather than value? In other words how can I get the address of my object? And how can I create a new object pointing to the same address?

And you could assume as well that by doing a.addDestination(b) it would add a to be too (although this is making the assumption that this is a bidirectional link which in airlines is a fairly safe but unnnecessary assumption)

Thanks a bunch.
[ April 07, 2007: Message edited by: David Brossard ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that I understand you. When you are dealing with classes and objects, you are already dealing with references. Same for arrays and other containers. If it is a hard absolute physicial address of an object that you are after, well you can't get it, at least not in any easy way that I know about (perhaps exceptions exist by doing some gymnastics w/ the JNI). But regardless since you are dealing w/ references there are very few cases where you'll ever need an absolute physical address. Also, I've heard that trying to manipulate absolute addresses can be very dangerous.

Show us code that you'd like to have work but breaks (and how it breaks). Maybe I'll have a better understanding then.
[ April 07, 2007: Message edited by: pete stein ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this work?:

and if not, what is broken?
[ April 07, 2007: Message edited by: pete stein ]
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well before asking I should have tried... What the code does is actually what I want. I never tried assuming it wouldn't.


This prints out the following:

Thanks for your perseverance and sorry for my ignorance
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!