# Creating a Data Structure to Hold an Array and Distance Value

• 04-01-2009, 01:20 AM
rdoane
Creating a Data Structure to Hold an Array and Distance Value
Hi all,
I would first like to thank your time.

Here is my problem, I need to make a data structure that looks similar to the picture attached.

It has to hold an array (which is a 5 element one dimensional array with integers). Also, at that same row index, it needs to hold a separate value which can have a different column index (assuming we are working with an array here for simplicity). I need to be able to access both elements for both printing and manipulating.

The code I have posted is what creates the array and integer value that needs to be stored along with it. The name of the array is cities[]. The array is altered on each iteration of the loop and once it is altered I would like to store it. Also, the value I would like to store along with the array in the same row is named "totalDistance".

Code:
package distanceproject;
import java.io.*;
import java.util.Random;

/**
*
* @author rdoane
*/

public class Main {

public static void main(String[] args) {
int i=0, j=0, k=0, l1=0, l2=0, randomCity1=0, randomCity2=0, temp1=0, temp2=0;
double totalDistance=0, optDistance=999;

Random r = new Random();
calcDistance d = new calcDistance();
calcTour t = new calcTour();

try{
// Open the file that is the first
FileInputStream fstream = new FileInputStream("C:/Users/Public/Documents/cities.txt");

// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);

//Read in the number of cities

double[][] locations=new double[Integer.parseInt(strLine)][Integer.parseInt(strLine)];

int [] cities = new int[locations.length];
int [] optCities = new int[locations.length];

//Due to some unseen errors this program's adaptability was sacrificed so it works.
cities[0]=4; cities[1]=2; cities[2]=0; cities[3]=3; cities[4]=1;

while ((strLine = br.readLine()) != null) {
double temp = Double.parseDouble(strLine);
locations[i][j]=temp ;
//System.out.println(locations[i][j]);
if(j<1)
j=j+1;
else
j=j-1;
if(j<1)
i=i+1;

}
in.close();

for (k=0;k<1000;k++)
{
randomCity1=r.nextInt(i);
randomCity2=r.nextInt(i);

temp1=cities[randomCity1];
temp2=cities[randomCity2];

cities[randomCity1]=temp2;
cities[randomCity2]=temp1;

totalDistance=t.calcTour(r, l1, l2, locations, cities, d);

}

}catch (Exception e){//Catch exception if any
}

}

}
• 04-01-2009, 01:24 AM
Fubarable
Quote:

Also, at that same row index, it needs to hold a separate value which can have a different column index
• 04-01-2009, 01:28 AM
rdoane
For example, if an array was used (and from all I've read it can't be) it would look like this:

array[1][1] = 1st array
array[1][2] = distance value corosponding to the array in array[1][1]
so forth and so on. So it would look like this when all is said and done:

Array, Int
Array, Int
Array, Int

Hope that clarifies.
• 04-01-2009, 01:30 AM
Fubarable
Why not then create a class that holds an array of 5 items and an int with appropriate getters and setters?
• 04-01-2009, 02:09 AM
rdoane
That was my initial though but it has been a long time since I have done Java and could not remember how to create a class which would do what I was looking for. Would you mind showing me how you would write it?
• 04-01-2009, 02:22 AM
Fubarable
You'll be much better off if you read up on it and first try to do this yourself.

You'll find all you need to know at the tutorials: The Sun Java Tutorials

Why not give it a look and then come back if you have any specific questions.

Best of luck.
• 04-01-2009, 02:56 AM
rdoane
I created a class which has methods for both setting and retrieving the array and integer value. I then filled an arraylist with each instance of that class.

Now I'm trying to retrieve each object of that class that is in the arraylist. However when I try to do the following method:

for i...
tempObject = arraylist.get(i);

I get an error. What is the best way for me to retrieve my object from the arraylist so I can call some of its methods?
• 04-01-2009, 02:59 AM
Fubarable
without the actual code and the error message, it's hard to know what you're doing wrong.
• 04-01-2009, 03:05 AM
rdoane
The class I created was called "tours". The arraylist is filled with objects of the tours class.

tours tempTour = new tours();
ArrayList tours =new ArrayList();

for(j=0;j<tours.size();j++)
{
tempTour = tours.get(i); <- Where I am getting the error.
tempTour.getDistance();
tempTour.printTour();
}

It says, Found: java.lang.Object, Required: distanceproject.tours

• 04-01-2009, 03:14 AM
Fubarable
I think that your problem is that the ArrayList is returning an Object and tempTour is expecting a tours object (note that your arraylist and your tours class have the same name, and this is confusing to me and the compiler -- so avoid this if you can). One way to solve this is to cast the returned Object to a tours object.

I would try to solve this by using a generic ArrayList here; something like this:
Code:

`ArrayList<tours> toursList = new ArrayList<tours>();`
Here I'm telling the compiler that this isn't any old ArrayList, this is an ArrayList that holds nothing but tours objects. Now when I call get(i) on this, I don't get an Object, I get a tours object.
• 04-01-2009, 03:39 AM
rdoane
Works perfectly. Thanks.
• 04-01-2009, 03:43 AM
Fubarable