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

Recommended Answers

All 9 Replies

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?

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

Here's an example--

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);
	}
}

. . . ok... slightly less confused... but how would i add a coordinate point to the array?

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

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.

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.

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.

u could consider vararg methods

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.