Results 1 to 12 of 12
- 04-01-2009, 01:20 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read in the number of cities
String strLine = br.readLine();
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
}
}
}
-
Please clarify.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 #3
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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.
-
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 #5
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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?
-
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 #7
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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?
-
without the actual code and the error message, it's hard to know what you're doing wrong.
- 04-01-2009, 03:05 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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
Thanks for your help.
-
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:
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.Java Code:ArrayList<tours> toursList = new ArrayList<tours>();
- 04-01-2009, 03:39 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
Works perfectly. Thanks.
-
Similar Threads
-
data structure and data base??
By ahmed13 in forum Advanced JavaReplies: 8Last Post: 03-27-2009, 05:48 AM -
int array - any other data structure that can handle large sets?
By RR_QQ in forum New To JavaReplies: 7Last Post: 02-11-2009, 09:14 PM -
data file structure
By Nicholas Jordan in forum Advanced JavaReplies: 2Last Post: 01-07-2009, 04:16 AM -
Queue data structure
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:35 PM -
data structure code
By vgvt in forum New To JavaReplies: 1Last Post: 01-17-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks