• Post Reply Bookmark Topic Watch Topic
  • New Topic

2D array and reversing it  RSS feed

 
ernest mcdermott
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone, i currently have a 2d array for distances between places. An example representation is:
Boston NewYork New Jersey
Boston 0 41 67
NewYork 41 0 21
NewJersey 67 21 0

The names are kept in a seperate array while the distances are stored in a 2D array.
So for example in the 2d array, [0][1] = 41 and [1][0] = 41.
Does any1 know how i can get it so that i dont have to write the values seperately everytime, so that the program automatically knows that [0][1] is equal to [1][0], [0][2] is equal to [2][0] etc etc
Thanks in advance
E
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a "rectangular" array in the first place?

You do realise there is no such thing as a 2D array in Java? It only has 1D arrays. What you think of as a 2D array isn't; it is a 1D array of 1D arrays!

You can get away with something like thisThat is newYorkToBoston
BostonToNewJersey NewYorkToNewJersey.

Slightly less intuitive to implement, but it avoids your duplication. You also miss out the [0][0] distance which is BostonToBoston.

[ edited for formatting corrections ]
[ March 06, 2007: Message edited by: Campbell Ritchie ]
 
ernest mcdermott
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell,
i used that way because i have to call the distances in another class. Im not great with java so how would you suggest i call them now???
Many thanks
E
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to do. But what about creating a java object and putting it in a List.

 
ernest mcdermott
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to create a minimal spanning tree. Im using adjacency matrix for input then with a city selected i want to select the distances to other cities, add them to a seperate array, compare them to find the smallest. Afterward, those which arent chosen are added to another new array whilst the smallest distance leads to the next city to be considered.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!