| | |
array help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 208
Reputation:
Solved Threads: 13
Hi, I need to create a huge array of (up to 160,000) points on a grid ex. (4, 23). First of all, I have no idea how to create an array, I looked on the Java API, but I was helplessly and hopelessly confused. And after a create the array, i need to be able to add more points to it, and check new points against old points to avoid repetition....
160,000 elements in an array can be memory-expensive... so let's try to find a way to make things less expensive.
First of all, what is the range of your points (what are they for, and what is the maximum number you're planning on using per coordinate)?
If the point-value is under 256, you can get away with making 2 arrays (one with x points, and one with y points) and have corresponding "vectors" based off of the index of both elements.
That or you can do it a potentially more expensive way by creating objects that hold the coordinate information. Depending on the Java Object Model, you may end up using more memory per object generated, especially since all methods in Java are virtual so you may want to use publicly accessible points in that object if you decide to go that route.
Then again, the more important thing right now is understanding what your range is for points. What's the limit, and do your points ever reach negative numbers, or are they unlimited naturals?
First of all, what is the range of your points (what are they for, and what is the maximum number you're planning on using per coordinate)?
If the point-value is under 256, you can get away with making 2 arrays (one with x points, and one with y points) and have corresponding "vectors" based off of the index of both elements.
That or you can do it a potentially more expensive way by creating objects that hold the coordinate information. Depending on the Java Object Model, you may end up using more memory per object generated, especially since all methods in Java are virtual so you may want to use publicly accessible points in that object if you decide to go that route.
Then again, the more important thing right now is understanding what your range is for points. What's the limit, and do your points ever reach negative numbers, or are they unlimited naturals?
Here's an example--
java Syntax (Toggle Plain Text)
public class Square{ public short x = 0, y = 0; public Square(short x, short y){ this.x = x; this.y = y; } public static void main(String... args){ Square.checkMemBefore(" - free mem before Square array"); Square lotsOfSquares[] = null; try{ lotsOfSquares = new Square[160000]; }catch(Exception e){ e.printStackTrace(); System.exit(1); } Square.checkMemAfter( " - free mem after Square array\n"); System.out.println("\n\n\n"); Square.checkMemBefore(" - free mem before allocating Square objects"); try{ for(int i = 0; i < lotsOfSquares.length; i++){ lotsOfSquares[i] = new Square((short)-200, (short)200); } }catch(Exception e){ e.printStackTrace(); System.exit(1); } Square.checkMemAfter(" - free mem after allocating Square objects"); } public static void checkMemBefore(String arg){ System.out.println(Runtime.getRuntime().freeMemory() + arg); } public static void checkMemAfter(String arg){ System.out.println("\n" + Runtime.getRuntime().freeMemory() + arg); } }
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
•
•
•
•
. . . ok... slightly less confused... but how would i add a coordinate point to the array?
Java Syntax (Toggle Plain Text)
lotsOfSquares[0] = new Square (5, 7); lotsOfSquares[1] = new Square (8, 9);
You may want to consider using a Vector or ArrayList rather than an array. Plus, Java has a Point class that already has an (x, y) pair so you don't have to make your own Square class. I think Alex made his own class because he was thinking of memory, so he figured shorts take up less memory than integers and the range of (-200, 200) all fits in a short. Rereading his post, it's pretty clear that is the reason. I'm a little confused between what's a square and what's a point and what the array size and what's the range of the values being stored. I noticed that 400 times 400 = 160,000, which is the size of the array.
Whoops, I'm incredibly sorry!
Yes Vernon is right. I confused points with squares! In this case you would have to use 4, not 2 points (or at least 4 kinds of data - if you know the starting point of the squares location and the length and width of the Square, you have fully defined it).
And I do believe Vernon is correct about the points. If you have 160,000 points divide the points by 4 and you have that many squares.
My class would need to be reworked - instead of 2 shorts, make 4 and accept 4 points from the user. You can make methods (final) such that they return the displacement between x's and y's to retrieve the distance and also the area.
Yes Vernon is right. I confused points with squares! In this case you would have to use 4, not 2 points (or at least 4 kinds of data - if you know the starting point of the squares location and the length and width of the Square, you have fully defined it).
And I do believe Vernon is correct about the points. If you have 160,000 points divide the points by 4 and you have that many squares.
My class would need to be reworked - instead of 2 shorts, make 4 and accept 4 points from the user. You can make methods (final) such that they return the displacement between x's and y's to retrieve the distance and also the area.
This example may help. Notice that you only need the starting point of the Square, relative to whatever plane it exists within, and the length of 1 side to determine the square--
EDIT: For mathematical and visual reasons, I made the square a square relative to a simple X:Y graph where Y is a function of X. It is not relative to the coordinate system that typical panes, in Java, adopt.
For you to obtain the same functionality in those panes you may find yourself adding instead of subtracting during the calculation of the other points within the 4 vertex-creating methods.
java Syntax (Toggle Plain Text)
public class Square{ public short x = 0, y = 0, length = 0; public Square(short x, short y, short length){ this.x = x; this.y = y; this.length = length; } public Square(Coord c, short length){ x = c.first; y = c.second; this.length = length; } public final void displayCoords(){ System.out.println(getULCoord() + "--" + getURCoord()); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("|\t\t\t |"); System.out.println("" + getLLCoord() + "--" + getLRCoord()); } public final class Coord{ short first = 0, second = 0; public Coord(short first, short second){ this.first = first; this.second = second; } @Override public String toString(){ return "< " + first + ", " + second + " >"; } } public final Coord getULCoord(){ return new Coord(x, y); } public final Coord getURCoord(){ return new Coord((short)(x + length), y); } public final Coord getLLCoord(){ return new Coord(x, (short)(y - length)); } public final Coord getLRCoord(){ return new Coord((short)(x + length), (short)(y - length)); } public static void main(String... args){ System.out.println("\n"); Square s1 = new Square((short)400, (short)500, (short)200); s1.displayCoords(); System.out.println("\n\n\n"); } public static void checkMemBefore(String arg){ System.out.println(Runtime.getRuntime().freeMemory() + arg); } public static void checkMemAfter(String arg){ System.out.println("\n" + Runtime.getRuntime().freeMemory() + arg); } }
EDIT: For mathematical and visual reasons, I made the square a square relative to a simple X:Y graph where Y is a function of X. It is not relative to the coordinate system that typical panes, in Java, adopt.
For you to obtain the same functionality in those panes you may find yourself adding instead of subtracting during the calculation of the other points within the 4 vertex-creating methods.
Last edited by Alex Edwards; Sep 20th, 2008 at 12:59 pm.
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Project title suggestions?
- Next Thread: Java Paint
Views: 709 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong







