13 Replies - 2374 Views - Last Post: 18 March 2013 - 03:56 AM Rate Topic: -----

#1 mouthwash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

creating an array of positions

Posted 16 March 2013 - 06:39 PM

How would i create an array that looked like this?
positions = [(702, 483), (912, 121), (203, 223), (427, 527), (548, 221), (924, 480)]
Is This A Good Question/Topic? 0
  • +

Replies To: creating an array of positions

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 16 March 2013 - 06:43 PM

You could use a Point[]. See the Point class.
Was This Post Helpful? 0
  • +
  • -

#3 mouthwash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: creating an array of positions

Posted 16 March 2013 - 06:48 PM

but that would mean having to set each x and y potition seperertly. I have A LOT of positions that I need to store and would much prefer to just put it in like this, since this is the output im getting for a map creator thing i made.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 16 March 2013 - 06:55 PM

So if you're creating a Map, you will need (x,y) coordinates. I don't see what the big deal is about setting the x and y coordinates on the Point objects. If you're writing your code correctly, you should be using a loop to read in the Points, so you shouldn't be manually instantiating a ton of Points.
Was This Post Helpful? 0
  • +
  • -

#5 mouthwash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: creating an array of positions

Posted 16 March 2013 - 07:01 PM

Could you please give me an example of how you would do it.
For example something like this:
[(629, 396), (568, 514), (249, 476), (242, 156), (728, 279), (1117, 641), (1026, 694), (876, 563), (1038, 262), (1025, 155), (608, 272), (414, 497), (301, 572), (330, 680), (504, 636), (638, 503), (690, 528), (718, 750), (947, 503), (933, 253), (788, 104), (469, 226), (256, 421), (224, 433), (210, 265), (427, 156), (483, 98), (702, 140), (866, 471), (1050, 421), (1089, 354), (1127, 605), (800, 696), (768, 681), (1139, 264), (1043, 119), (248, 479), (198, 673), (504, 614), (747, 426), (834, 361), (536, 297), (278, 492), (349, 345), (520, 278), (433, 429), (429, 515), (612, 213), (781, 137), (712, 119), (416, 297), (169, 181), (206, 135), (25, 489), (186, 663), (54, 251), (69, 122), (118, 537), (90, 717), (178, 468), (682, 307), (855, 527), (415, 726), (677, 643), (821, 347), (990, 53), (1049, 203), (986, 573), (966, 316), (946, 235), (861, 74), (562, 487), (950, 323), (1006, 182), (835, 315), (687, 155), (229, 235), (517, 378), (665, 86), (492, 104), (96, 27), (336, 553), (475, 193), (373, 129), (477, 416), (681, 543), (891, 493), (960, 582), (1165, 451), (1057, 673), (890, 726), (634, 634), (811, 373), (754, 279), (647, 532), (777, 436), (812, 523), (846, 659), (1016, 434), (1089, 283), (1123, 120), (1125, 116), (781, 128), (368, 175), (357, 463), (664, 193), (387, 277), (290, 290), (431, 145), (74, 443), (385, 678), (928, 449), (589, 648), (575, 690), (566, 718), (439, 590), (369, 609), (990, 605), (1278, 388), (1204, 160), (1143, 80), (1006, 106), (1164, 292), (1204, 565), (1174, 654), (1202, 232), (1182, 107), (1209, 129), (1269, 315), (1251, 653), (1163, 696), (1098, 524), (1054, 485), (1049, 545)]



how would i put that information into points?

And i dont want to java file to be reading from the text file. I want it only in the code.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 16 March 2013 - 07:01 PM

Where are you storing this information? A File? If so, just read the File, parse the Points and instantiate new Points dynamically. I'll let you read up on File I/O.
Was This Post Helpful? 0
  • +
  • -

#7 mouthwash   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: creating an array of positions

Posted 16 March 2013 - 08:36 PM

View Postmacosxnerd101, on 16 March 2013 - 07:01 PM, said:

Where are you storing this information? A File? If so, just read the File, parse the Points and instantiate new Points dynamically. I'll let you read up on File I/O.

Okay, maybe I haven't been clear. Sorry about that.
What I want is to just have an array variable holding those positions. I DO NOT want to pull the information from a text document. That means I don't want to do any file I/O, since I already know how to do that. I also dont want to have to go through and say
point[0].x = 5322
point[0].y = 32423
cause that would take far too long.
Basically I want to make an array of tuples. How would I go about doing this?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 16 March 2013 - 08:43 PM

An important part of software design is separating out the data from the state. If you hard-code all your points in, then initializing them is going to be a pain. Java doesn't have a tuple structure like other languages do. The Point class is really an appropriate tool here. You have to work to separate out the data so that you can cleanly import it. If you hard code these values, it will be a tedious process to organize the data one way or the other.

Perhaps a slightly different question- why must you hard code these values?
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7260
  • View blog
  • Posts: 15,141
  • Joined: 16-October 07

Re: creating an array of positions

Posted 17 March 2013 - 03:34 AM

You seem to be missing the use of constructors. Anyway, it's just syntax. You can write methods to help you.

Option 1, using an array:
Point[] positions1 = {
	new Point(702, 483), new Point(912, 121), new Point(203, 223),
	new Point(427, 527), new Point(548, 221), new Point(924, 480),};



That's kind of brute force and arrays aren't that flexible. Making your own class, or at least using a collection, it usually a better choice.

Option 2:
class LotsAPoints extends ArrayList<Point> {
	// methods are your friends
	// never underestimate the power of a convenience method
	public LotsAPoints add(int x, int y) {
		this.add(new Point(x, y));
		return this;
	}
}

// initialized same as array
LotsAPoints positions2 = new LotsAPoints()
	.add(702, 483).add(912, 121).add(203, 223)
	.add(427, 527).add(548, 221).add(924, 480);



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 17 March 2013 - 04:03 PM

Extending ArrayList really has the Java 1.4.2 feel, back in the days before generics. It would probably be better to encapsulate the ArrayList and provide restricted access. :)
Was This Post Helpful? 0
  • +
  • -

#11 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7260
  • View blog
  • Posts: 15,141
  • Joined: 16-October 07

Re: creating an array of positions

Posted 17 March 2013 - 04:28 PM

It IS a generic ArrayList, note the diamond notation with the type in it. :P/>

I was trying to keep it simple. However, if you want to be pedantic about it and offer least required access, we could go with:
class LotsAPoints implements Iterable<Point> {
	private final java.util.List<Point> innerList;
	public LotsAPoints() { this.innerList = new ArrayList<>(); }

	public LotsAPoints add(int x, int y) {
		this.innerList.add(new Point(x, y));
		return this;
	}

	@Override
	public Iterator<Point> iterator() { return this.innerList.iterator(); }
}



This is the way I might do it, but it does introduce other questions if the user is unfamiliar with Java.

This post has been edited by baavgai: 17 March 2013 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • Joined: 27-December 08

Re: creating an array of positions

Posted 17 March 2013 - 04:37 PM

Quote

It IS a generic ArrayList, note the diamond notation with the type in it. :P

I saw. :) Back in the days before generics, though, Java programmers would extend Collections to emulate generics, so to speak. So developers had libraries with StringList, IntegerList, etc., all extending some sort of Collection. I feel like if a Collection is being extended for a specific purpose, such as to model a map in a game (as is the OP's case), then it's probably a case for composition rather than inheritance. I'd probably argue against the Iterator too, for providing too little access. If a Map has-a List<Point>, then it should provide the functionality of a Map, not necessarily random access to the Points themselves.
Was This Post Helpful? 0
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: creating an array of positions

Posted 17 March 2013 - 04:59 PM

View Postmouthwash, on 16 March 2013 - 10:01 PM, said:

Could you please give me an example of how you would do it.
For example something like this:
[(629, 396), (568, 514), (249, 476), (242, 156), (728, 279), (1117, 641), (1026, 694), (876, (1049, 545)]



how would i put that information into points?

And i dont want to java file to be reading from the text file. I want it only in the code.

Like this being what ?
Code already entered in your .java source file ?

The way it is entered really easy to convert it into an Array of int[][] or an array of Point[]
Was This Post Helpful? 0
  • +
  • -

#14 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7260
  • View blog
  • Posts: 15,141
  • Joined: 16-October 07

Re: creating an array of positions

Posted 18 March 2013 - 03:56 AM

View Postmacosxnerd101, on 17 March 2013 - 06:37 PM, said:

I'd probably argue against the Iterator too, for providing too little access.


There really is no pleasing you. ;)

Seriously, though, you essentially give minimum access until you need more. An Iterator is least, to start. Pretty much all other access, like a getter, a setter, contains, etc, would be methods you'd add to the class: when you need them. Adding them before you need them is a waste and usually poor design.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1