Results 1 to 12 of 12
  1. #1
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default 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
    }

    }

    }
    Attached Thumbnails Attached Thumbnails Creating a Data Structure to Hold an Array and Distance Value-example.jpg  

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, at that same row index, it needs to hold a separate value which can have a different column index
    Please clarify.

  3. #3
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not then create a class that holds an array of 5 items and an int with appropriate getters and setters?

  5. #5
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    without the actual code and the error message, it's hard to know what you're doing wrong.

  9. #9
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    Java 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.

  11. #11
    rdoane is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Works perfectly. Thanks.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  2. Replies: 7
    Last Post: 02-11-2009, 09:14 PM
  3. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 04:16 AM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:35 PM
  5. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •