array help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

array help

 
0
  #1
Sep 19th, 2008
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....
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: array help

 
0
  #2
Sep 19th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: array help

 
0
  #3
Sep 19th, 2008
the x values go from -200 to 200, and the same goes for the y vaules, -200 to 200. So what i have is a square that has a width and length of 400. and i think that answers your questions >.<
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: array help

 
0
  #4
Sep 19th, 2008
Here's an example--

  1. public class Square{
  2.  
  3. public short x = 0, y = 0;
  4.  
  5. public Square(short x, short y){
  6. this.x = x;
  7. this.y = y;
  8. }
  9.  
  10. public static void main(String... args){
  11.  
  12. Square.checkMemBefore(" - free mem before Square array");
  13. Square lotsOfSquares[] = null;
  14. try{
  15. lotsOfSquares = new Square[160000];
  16. }catch(Exception e){
  17. e.printStackTrace();
  18. System.exit(1);
  19. }
  20.  
  21. Square.checkMemAfter( " - free mem after Square array\n");
  22.  
  23. System.out.println("\n\n\n");
  24.  
  25. Square.checkMemBefore(" - free mem before allocating Square objects");
  26.  
  27. try{
  28. for(int i = 0; i < lotsOfSquares.length; i++){
  29. lotsOfSquares[i] = new Square((short)-200, (short)200);
  30. }
  31. }catch(Exception e){
  32. e.printStackTrace();
  33. System.exit(1);
  34. }
  35.  
  36. Square.checkMemAfter(" - free mem after allocating Square objects");
  37. }
  38.  
  39. public static void checkMemBefore(String arg){
  40. System.out.println(Runtime.getRuntime().freeMemory() + arg);
  41. }
  42.  
  43. public static void checkMemAfter(String arg){
  44. System.out.println("\n" + Runtime.getRuntime().freeMemory() + arg);
  45. }
  46. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: array help

 
0
  #5
Sep 19th, 2008
. . . ok... slightly less confused... but how would i add a coordinate point to the array?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: array help

 
0
  #6
Sep 19th, 2008
Originally Posted by llemes4011 View Post
. . . ok... slightly less confused... but how would i add a coordinate point to the array?
From the above example, if you wanted to "add" the points (5, 7) and (8, 9), you could do this:

  1. lotsOfSquares[0] = new Square (5, 7);
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: array help

 
0
  #7
Sep 19th, 2008
ok, that helped, I'm going to look into the Vector and ArrayList stuff and see what I can dig up

I'm going to leave this as unsolved, incase i have more questions pertaining to this subject, so, if anyone wants to chime in, feel free.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: array help

 
0
  #8
Sep 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: prateeknigamk is an unknown quantity at this point 
Solved Threads: 0
prateeknigamk prateeknigamk is offline Offline
Newbie Poster

Re: array help

 
0
  #9
Sep 20th, 2008
u could consider vararg methods
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: array help

 
0
  #10
Sep 20th, 2008
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--

  1. public class Square{
  2.  
  3. public short x = 0, y = 0, length = 0;
  4.  
  5. public Square(short x, short y, short length){
  6. this.x = x;
  7. this.y = y;
  8. this.length = length;
  9. }
  10.  
  11. public Square(Coord c, short length){
  12. x = c.first;
  13. y = c.second;
  14. this.length = length;
  15. }
  16.  
  17. public final void displayCoords(){
  18. System.out.println(getULCoord() + "--" + getURCoord());
  19. System.out.println("|\t\t\t |");
  20. System.out.println("|\t\t\t |");
  21. System.out.println("|\t\t\t |");
  22. System.out.println("|\t\t\t |");
  23. System.out.println("|\t\t\t |");
  24. System.out.println("|\t\t\t |");
  25. System.out.println("|\t\t\t |");
  26. System.out.println("|\t\t\t |");
  27. System.out.println("" + getLLCoord() + "--" + getLRCoord());
  28. }
  29.  
  30. public final class Coord{
  31. short first = 0, second = 0;
  32.  
  33. public Coord(short first, short second){
  34. this.first = first;
  35. this.second = second;
  36. }
  37.  
  38. @Override public String toString(){
  39. return "< " + first + ", " + second + " >";
  40. }
  41. }
  42.  
  43. public final Coord getULCoord(){
  44. return new Coord(x, y);
  45. }
  46.  
  47. public final Coord getURCoord(){
  48. return new Coord((short)(x + length), y);
  49. }
  50.  
  51. public final Coord getLLCoord(){
  52. return new Coord(x, (short)(y - length));
  53. }
  54.  
  55. public final Coord getLRCoord(){
  56. return new Coord((short)(x + length), (short)(y - length));
  57. }
  58.  
  59. public static void main(String... args){
  60. System.out.println("\n");
  61. Square s1 = new Square((short)400, (short)500, (short)200);
  62. s1.displayCoords();
  63. System.out.println("\n\n\n");
  64. }
  65.  
  66. public static void checkMemBefore(String arg){
  67. System.out.println(Runtime.getRuntime().freeMemory() + arg);
  68. }
  69.  
  70. public static void checkMemAfter(String arg){
  71. System.out.println("\n" + Runtime.getRuntime().freeMemory() + arg);
  72. }
  73. }

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 709 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC