9 Replies - 3037 Views - Last Post: 08 January 2010 - 01:12 AM Rate Topic: -----

#1 Serena Feng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-January 10

Convert object array to int array

Post icon  Posted 06 January 2010 - 09:02 PM

hi, I need some help relating to arraylist and array. Already done some google stuff, and still cannot figure out. Please help :)

My aim is basically read csv file to an arraylist(columns) and convert the arraylist to array(arrObj), then use the array to build polygons.

Object[] arrObj = columns.toArray( );


However, to define a polygon, only int points or int arrays can be used.

So how should I convert the object array arrObj to an int array.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Convert object array to int array

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: Convert object array to int array

Posted 06 January 2010 - 09:26 PM

If the ArrayList is of generic-type Integer (ArrayList<Integer>), then you could use an Integer array instead of an Object array.
Was This Post Helpful? 0
  • +
  • -

#3 Serena Feng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-January 10

Re: Convert object array to int array

Posted 06 January 2010 - 09:39 PM

View Postmacosxnerd101, on 6 Jan, 2010 - 08:26 PM, said:

If the ArrayList is of generic-type Integer (ArrayList<Integer>), then you could use an Integer array instead of an Object array.


Actually, the arraylist I defined is (ArrayList<String>). Then what kind of array should I use?

The following is my code:
import java.lang.String.;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.String.;
import java.util.ArrayList;
import java.awt.Polygon;

public class Polygon1 {

public void arrayofPolygon() {

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader("Data/testing.csv"));
String line = null;
ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();

int lineNum = 0;
while ((line = br.readLine()) != null) {
+lineNum;

String values[] = line.split(",");

int columnNum = 0;
for(String str: values){

if(columnNum >= columns.size()){

int oldSize = columns.size();
for(int i = 0; i <= oldSize - columnNum; ++i){

columns.add(new ArrayList<String>());
}
}
columns.get(columnNum).add(str);
++columnNum;

}

}//while

Object[] arrObj = columns.toArray( );

Polygon polyx[]=new Polygon[columns.size()/2];

for ( int i = 0; i < columns.size(); i+ )
{
Object[] array0 = ((ArrayList)arrObj[i]).toArray();
Object[] array1 = ((ArrayList)arrObj[i+1]).toArray(); //need to cast the arraylist then apply the toArray
int n = ((ArrayList)arrObj[i]).toArray().length;
polyx[i] = Polygon(array0,array1,n);
}


}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {

Polygon1 test = new Polygon1();
test.arrayofPolygon();

}

}

This post has been edited by Serena Feng: 06 January 2010 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: Convert object array to int array

Posted 06 January 2010 - 10:01 PM

Please properly indent your code so it is easier for us to read, and therefore debug.

After glancing over your code, I think a lot of your problem is that you are working with a 2D ArrayList, but treating it as if it was one-dimmensional. I'm guessing each line contains a list of (x,y) coordinates for a polygon, or does each line contain an x and a y value? I will need more information on the File format before I can help you develop your logic for it.
Was This Post Helpful? 0
  • +
  • -

#5 Serena Feng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-January 10

Re: Convert object array to int array

Posted 06 January 2010 - 10:21 PM

Sorry, I am new to java and not sure the correct way of indenting code, apologize for the inconvenience caused.

I have attached a file which I read data from. It is originally a csv file, but cannot upload, so just changed to txt file.

The arraylist is a 2D arraylist, and it will be something like this: [[1, 2, 2, 5, 8, 2, 6, 2], [4, 45, 5, 1, 2, 8, 6, 9], [45, 45, 45, 45], [45, 7, 5, 2]].

I want to use the first and second columns as the x and y coordinates for polygon1, third and fourth columns
as the x and y coordinates for polygon2.

Thanks. :rolleyes:

Attached File(s)

  • Attached File  test.txt (66bytes)
    Number of downloads: 58

Was This Post Helpful? 0
  • +
  • -

#6 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Convert object array to int array

Posted 07 January 2010 - 05:11 AM

You can do something like:
int[][] coordinates8Length = {{1, 2, 2, 5, 8, 2, 6, 2}, {4, 45, 5, 1, 2, 8, 6, 9}};
int[][] coordinates4Length = {{45, 45, 45, 45}, {45, 7, 5, 2}};
int[] polygon1XCoordinates = coordinates8Length[0];
int[] polygon1YCoordinates = coordinates8Length[1];
int[] polygon2XCoordinates = coordinates4Length[0];
int[] polygon2YCoordinates = coordinates4Length[1];


Your problem is manipulating arrays or reading the file?

This post has been edited by anonymouscodder: 07 January 2010 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: Convert object array to int array

Posted 07 January 2010 - 07:25 AM

Using a static array isn't a good idea when reading in data from a File because you usually don't know how many tokens are in the File.

@OP: I would suggest instead writing your own Point class that contains int attributes for the x and y coordinates (the one in the API uses doubles or floats, not ints). Then, just store an ArrayList<Points>. From there, it should be easy enough to create an int[] x and int[] y to create a Polygon.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2685
  • View blog
  • Posts: 11,345
  • Joined: 20-September 08

Re: Convert object array to int array

Posted 07 January 2010 - 07:37 AM

Your file format is curious. Each line should probably should represent one polygon, with each pair of numbers representing the coordinates of one vertex
Was This Post Helpful? 0
  • +
  • -

#9 Serena Feng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-January 10

Re: Convert object array to int array

Posted 07 January 2010 - 04:25 PM

View Postanonymouscodder, on 7 Jan, 2010 - 04:11 AM, said:

Your problem is manipulating arrays or reading the file?


hi,

I have already done the reading csv file part, and store it into the arraylists. The problem now is how to convert the arraylist to array which can be used to build polygons. Since I do not know how many tokens will be in the file, I cannot use the code you mentioned. Thank you all the same. :P
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2685
  • View blog
  • Posts: 11,345
  • Joined: 20-September 08

Re: Convert object array to int array

Posted 08 January 2010 - 01:12 AM

If you change your data format to the one i mentioned, your code, with which i can help you, can become simpler
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1