so i'm trying to setup an array that holds x y coordinates.
the program seems to work but my print results are memory addresses.
here's my code:
static class Point{
int x;
int y;
#Override
public String toString() {
return x + " " + y;
}
}
public static Object thePoints( int x, int y){
Point[] mypoints = new Point[10];
for (int i = 0; i < mypoints.length; i++){
mypoints[i] = new Point();
}
mypoints[0].x = 200;
mypoints[0].y = 200;
return mypoints;
}
public static void main(String args[]) {
Object thing = thePoints(0,0);
System.out.print(thing);
}
}
input is appreciated.
You are printing out an array of type Point[] in your main() method, contrary to what it appears. A quick way to get around this problem is to use Arrays.toString(). Try changing the code in your main() method to this:
public static void main(String args[]){
Object thing = thePoints(0,0);
System.out.print(Arrays.toString((Point[])thing));
}
If you also refactor Point.toString() to the following, then you get some fairly nice looking output:
static class Point{
int x, y;
#Override
public String toString() {
return "(" + x + ", " + y + ")"; // print out a formatted ordered pair
}
}
Output:
[(200, 200), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
You are trying to print an array of Point Objects which is why it prints out the memory address. You can use an ArrayList instead of an array. Printing an array list would call the toString method of each element which in you case is the toString method of Point Class.
Your printing the 'Object thing' directly. System.out.print() only works on strings and primitive datatypes, so if you wanted to print information about the 'Object thing' you would have to use the toString() method you declared for the class 'Point'.
System.out.println(thing[0].toString());
I have a Player class which has
int number;
In main, I store them in Array.
Array<Player> players;
How can I get a player which has for example number=2?
This question is programming in general, and has several ways to do what you say, I recommend you look for OOP.
a solution, general, would encapsulate the variable, "creating getter and setter, you need, eg:
this a simple class;
private int number;
public Player(int num){
this.number = num;
}
public int getNumber (){
return number;
}
public void setNumber (int n){
this.number = n;
}
.
In your code for search you can use the solution, using a for, or an iterator
for (int a = 0; a < players.size(); a++){
int tmpNumber = players.get(a).getNumber();
if(tmpNumber == 2){
players.get(a); //the object array with index equal to
//the value of 'a', have, number 2 stored
//in the variable 'number'
}
}
but this question I think is a little matter of taste or needs you have
I'm going to make some assumptions to demonstrate it:
Let's assume you have a constructor in your Player class:
public Player(int num){
this.number = num;
}
And you have added some players in your arraylist:
players.add(new Player(1));
players.add(new Player(2));
players.add(new Player(3));
Now loop through your array and find the one where number == 2
for (int i = 0; i < players.size(); i++){
Player tmp = players.get(i);
if(tmp.number == 2){
System.out.println("Index of player number 2: " + players.indexOf(tmp));
}
}
I realised that I'd misunderstood the question with my previous answer. Here's a better one...
Those recommending you iterate through the Array are correct, but if you have a lot of Players this will be ineffeicient. In that case, it would be better to use a Map where the key was the player number, and the value is the player itself.
Here's some sample code showing the approach (most of it is just setting up some test data)...
import java.util.HashMap;
import java.util.Map;
public class Temp {
public static void main(String[] args) {
// Create a map instead of an array
Map<Integer, Player> players = new HashMap<>();
// Quick hack just to put some data in it.
for(int i = 0; i < 1000; i++) {
players.put(i, new Player(i));
}
// Once they're in a map, retrieving them is simple...
Player playerFromMap = players.get(537);
// Check we got it right
System.out.println(playerFromMap.getNumber()); // 537
}
private static class Player {
private int number;
private Player(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
}
I am creating a game using an array, I have my Hunter class which looks somewhat like this ;
public static int x= 11;
public static int y =11;
public static String name = "H";`
And a method for its path using x and y.
I have declared hunter as an array in my board (2d array) class this way;
public Hunter hunters[] = new Hunter[5];
and the position of a hunter is declared in the board class as ;
a2[Hunter.x][Hunter.y] = Hunter.name;
Question: I want 5 hunters to appear on the board, how do I use the array to spawn additional 4 hunters? Thanks.
you created your array fine all you need to do is use it:
for (int i = 0; i < 5; ++i)
{
a2[hunters[i].x][hunters[i].y] = hunters[i].name
}
also, you need to make your Hunter members non-static
class Hunter
{
private int x, y;
public void setLocation(int x_, int y_)
{
x = x_; y = y_;
}
}
you get the idea :)
The static keyword (assuming you are using C++, java, C#) means that the variable is shared among all instances of the Hunter class. To allow each Hunter to have its own position, remove the static keyword and initialize them with a constructor.
I'll assume you're using Java bases on your use of String:
public class Hunter {
public int x;
public int y;
public String name;
public Hunter(int x, int y, string name) {
this.x = x;
this.y = y;
this.name = name;
}
}
Then to initialize 5 you would do
int numHunters = 5;
for (int i = 0; i < numHunters; i ++) {
hunters[i] = new Hunter(/* put x and y and name here */);
}
You can then use them to populate the board:
for (int i = 0; i < numHunters; i ++) {
Hunter h = hunters[i];
a2[h.x][h.y] = h.name;
}
You didn't specify what language you're using. That would help a bit.
At a minimum, try removing the "static" keyword from your property definitions.
In C#, your Hunter class might look like
public class Hunter
{
public int x;
public int y;
public String name;
public Hunter(int newX, int newY, String newName)
{
x = newX;
y = newY;
name = newName;
}
}
You create new Hunters using Hunter h1 = new Hunter(11, 11, "H");. Once created, you can do whatever with it.
You may want to do some reading up on Object Oriented Programming - see Intro to OOP esp sections 4.3 - 4.5 (they're short)
I am writing a web application using GWT and App Engine. My application will need to post and query items based on their latitude, longitude.
As a result of google's distributed database design you can't simple query a set of inequalities. Instead they suggest doing geohashing. The method is described on this page.
http://code.google.com/appengine/articles/geosearch.html
Essentially you pre compute a bounding box so that you can query items that have been tagged with that bounding box.
There is one part of the process that I don't understand. What does the "slice" attribute mean?
Thanks for your help!
For a complete java portage of Geomodel, please see http://code.google.com/p/javageomodel/.
There is a demo class to explain you how to use it.
Rather than implementing the geohash yourself, you might be interested in the GeoModel open source project that implements a geohash-like system on Google App Engine. Rather than understanding all the details, you can just import this library and make calls like proximity_fetch() and bounding_box_fetch().
This more recent article describes how it works and provides an example that uses it.
Instead of defining a bounding box with 4 coordinates (min and max latitude, min and max longitude), you can define it with the coordinates of the North-West corner of the box, and two parameters : resolution and slice.
The resolution defines the scale of the box, it is implemented as the number of figures below the decimal-point.
The slice is the width and height of the box, using the least significant figure as its unit.
The comments in geobox.py explain this in more details, with good examples :
To query for members of a bounding box, we start with some input coordinates
like lat=37.78452 long=-122.39532 (both resolution 5). We then round these
coordinates up and down to the nearest "slice" to generate a geobox. A "slice"
is how finely to divide each level of resolution in the geobox. The minimum
slice size is 1, the maximum does not have a limit, since larger slices will
just spill over into lower resolutions (hopefully the examples will explain).
Some examples:
resolution=5, slice=2, and lat=37.78452 long=-122.39532:
"37.78452|-122.39532|37.78450|-122.39530"
resolution=5, slice=10, and lat=37.78452 long=-122.39532:
"37.78460|-122.39540|37.78450|-122.39530"
resolution=5, slice=25, and lat=37.78452 long=-122.39532:
"37.78475|-122.39550|37.78450|-122.39525"
I'm working on a GWT/GAE project and had the same problem. My solution was to use a Geohash class that I modified slightly to be GWT-friendly. It's great for my needs of proximity searches.
If you've never seen Geohashes in action, check out Dave Troy's JS demo page.
An alternative to do geo spatial searches in App Engine is the Search Api. You won't need to worry about geohashing or implementation details, and you'll be able to search for elements close to geo point.
https://developers.google.com/appengine/docs/python/search/overview#Performing_Location-Based_Searches
I was working on a GAE project with geohashing and this python library did the trick for me: http://mappinghacks.com/code/geohash.py.txt
I was also in need of a Java version of GeoModel. I was working with Geohash before, which allowed me to fetch locations in a given bounding box. But there are considerable limitations to this when it comes to sorting: in order to get BigTable to accept a filter like geohash > '" + bottomLeft + "' && geohash < '" + topRight + "'", you have to order the list by geohash as well, which makes it impossible to sort it by other criteria (especially if you want to use pagination). At the same time, I just can't think of a solution to sort the results by distance (from a given user-position, i.e. the center of the bounding box), other than in Java-code. Again, this will not work if you need to have pagination.
Because of these problems I had to use a different approach, and GeoModel/Geoboxes seemed to be the way. So, I ported the Python-code to Java and it's just working fine! Here is the result:
public class Geobox {
private static double roundSlicedown(double coord, double slice) {
double remainder = coord % slice;
if (remainder == Double.NaN) {
return coord;
}
if (coord > 0) {
return coord - remainder + slice;
} else {
return coord - remainder;
}
}
private static double[] computeTuple(double lat, double lng,
int resolution, double slice) {
slice = slice * Math.pow(10, -resolution);
double adjustedLat = roundSlicedown(lat, slice);
double adjustedLng = roundSlicedown(lng, slice);
return new double[] { adjustedLat, adjustedLng - slice,
adjustedLat - slice, adjustedLng };
}
private static String formatTuple(double[] values, int resolution) {
StringBuffer s = new StringBuffer();
String format = String.format("%%.%df", resolution);
for (int i = 0; i < values.length; i++) {
s.append(String.format(format, values[i]).replace(',','.'));
if (i < values.length - 1) {
s.append("|");
}
}
return s.toString();
}
public static String compute(double lat, double lng, int resolution,
int slice) {
return formatTuple(computeTuple(lat, lng, resolution, slice),
resolution);
}
public static List<String> computeSet(double lat, double lng,
int resolution, double slice) {
double[] primaryBox = computeTuple(lat, lng, resolution, slice);
slice = slice * Math.pow(10, -resolution);
List<String> set = new ArrayList<String>();
for (int i = -1; i < 2; i++) {
double latDelta = slice * i;
for (int j = -1; j < 2; j++) {
double lngDelta = slice * j;
double[] adjustedBox = new double[] { primaryBox[0] + latDelta,
primaryBox[1] + lngDelta, primaryBox[2] + latDelta,
primaryBox[3] + lngDelta };
set.add(formatTuple(adjustedBox, resolution));
}
}
return set;
}
}
sorry for the late answer, but I didn't return to this page for some time. A GeoDao implementation using the Geobox approach could look like this:
public class GeoDaoImpl extends DaoImpl<T extends GeoModel> {
// geobox configs are: resolution, slice, use set (1 = true)
private final static int[][] GEOBOX_CONFIGS =
{ { 4, 5, 1 },
{ 3, 2, 1 },
{ 3, 8, 0 },
{ 3, 16, 0 },
{ 2, 5, 0 } };
public GeoDaoImpl(Class<T> persistentClass) {
super(persistentClass);
}
public List<T> findInGeobox(double lat, double lng, int predefinedBox, String filter, String ordering, int offset, int limit) {
return findInGeobox(lat, lng, GEOBOX_CONFIGS[predefinedBox][0], GEOBOX_CONFIGS[predefinedBox][1], filter, ordering, offset, limit);
}
public List<T> findInGeobox(double lat, double lng, int resolution, int slice, String filter, String ordering, int offset, int limit) {
String box = Geobox.compute(lat, lng, resolution, slice);
if (filter == null) {
filter = "";
} else {
filter += " && ";
}
filter += "geoboxes=='" + box + "'";
return super.find(persistentClass, filter, ordering, offset, limit);
}
public List<T> findNearest(final double lat, final double lng, String filter, String ordering, int offset, int limit) {
LinkedHashMap<String, T> uniqueList = new LinkedHashMap<String, T>();
int length = offset + limit;
for (int i = 0; i < GEOBOX_CONFIGS.length; i++) {
List<T> subList = findInGeobox(lat, lng, i, filter, ordering, 0, limit);
for (T model : subList) {
uniqueList.put(model.getId(), model);
}
if (uniqueList.size() >= length) {
break;
}
}
List<T> list = new ArrayList<T>();
int i = 0;
for (String key : uniqueList.keySet()) {
if (i >= offset && i <= length) {
list.add(uniqueList.get(key));
}
i++;
}
Collections.sort(list, new Comparator<T>() {
public int compare(T model1, T model2) {
double distance1 = Geoutils.distFrom(model1.getLatitude(), model1.getLongitude(), lat, lng);
double distance2 = Geoutils.distFrom(model2.getLatitude(), model2.getLongitude(), lat, lng);
return Double.compare(distance1, distance2);
}
});
return list;
}
#Override
public void save(T model) {
preStore(model);
super.save(model);
}
private void preStore(T model) {
// geoboxes are needed to find the nearest entities and sort them by distance
List<String> geoboxes = new ArrayList<String>();
for (int[] geobox : GEOBOX_CONFIGS) {
// use set
if (geobox[2] == 1) {
geoboxes.addAll(Geobox.computeSet(model.getLatitude(), model.getLongitude(), geobox[0], geobox[1]));
} else {
geoboxes.add(Geobox.compute(model.getLatitude(), model.getLongitude(), geobox[0], geobox[1]));
}
}
model.setGeoboxes(geoboxes);
}
}