java - Finding the nth nearest object for two datatypes -


i'm trying find nth nearest object player , have looked around , come conclusion 2d arraylist or list seems need store object id's , distances player, sorting ascending order. i'm not sure how achieve lists. have working code find nearest object finding nth seems trickier without using lot of variables.

this question below closest answer i've seen - use 2 strings , not 2 different values such object , int need.

how sort 2d arraylist<string> first element

list<arraylist<gameobject>> nearest = new arraylist<arraylist<gameobject>>();  nearest.add(new arraylist<gameobject>(arrays.aslist(instance, int))); //adds new instance , it's distance player. 

currently i'm getting error i'm not allowed both object , int in array , can't seem define both types.

let's have collection (set, list, whatever) of gameobject:

collection<gameobject> gameobjects = ...; 

you have, somewhere, method used compute distance of 1 gameobject player. i'll assume returns int:

public int computedistancetoplayer(gameobject gameobject) {     ... } 

you want sort these gameobjects distance player, in order nth closer object. easiest way sort objects. example:

list<gameobject> sortedgameobjects =      gameobjects.stream()                .sorted(comparator.comparingint(gameobject -> computedistancetoplayer(gameobject)))                .collect(collectors.tolist()); 

you can nth element list.

you can nth element stream directly:

gameobject nthclosergameobject =      gameobjects.stream()                .sorted(comparator.comparingint(gameobject -> computedistancetoplayer(gameobject)))                .skip(n - 1)                .findfirst()                .orelse(null); 

that need, but, if distance computation costly (requires long, costly computation), it's not optimal because computes distance of same gameobject several times: each time it's compared gameobject during sorting. so, if want avoid that, can compute distances first , associate them gameobjects, , sort resulting results:

public class gameobjectwithdistance {     private final gameobject gameobject;     private final int distance;      // constructor, getters omitted brevity } 

now need wrap every gameobject inside gameobjectwithdistance, , sort result:

gameobject nthclosergameobject =      gameobjects.stream()                .map(gameobject -> new gameobjectwithdistance(gameobject, computedistancetoplayer(gameobject)))                .sorted(comparator.comparingint(gameobjectwithdistance::getdistance))                .skip(n - 1)                .findfirst()                .map(gameobjectwithdistance::getgameobject)                .orelse(null); 

now, if you're unfamiliar streams , lambdas, can loops, lists , comparator class, doesn't matter. matters logic, , realization don't need 2d list of that.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -