Lenny Carmi
Lenny Carmi

Reputation: 783

How can I create copy of a libgdx array with custom objects?

I have an array which contains values pawnArray. I need to find the highest value in pawnArray so using a custom class method getPawn() I retrieve the highest value but I do

    public static Pawn getPawn(Array<Pawn> strollpawns) {
    Array<Pawn> pawns = strollpawns;
    pawns.sort();
    Pawn best = pawns.get(0);

    return best;
}

I hence need to copy the array since this method doesn't work. How can I make a copy of this array?

Upvotes: 1

Views: 1043

Answers (4)

mewiki
mewiki

Reputation: 135

Answer to your question: How can I create copy of a libgdx array

Array<Pawn> pawns = new Array<Pawn>(strollpawns);

or if the pawns Array object already exists

pawns.clear();
pawns.addAll(strollpawns);

The first solution will create a new Array object that will be deleted on completion of the function, meaning time lost by garbage collector!

But I agree with Tenfour04: Duplicating an array and sorting it is a very expensive way to select the biggest value.

Upvotes: 0

Javier Mart&#237;n
Javier Mart&#237;n

Reputation: 2605

I'm adding a separate answer to this, since you want to copy your array and sort it in order to retrieve the highest value. My other answer deals with copying the array, while tjago's answer deals with sorting with a custom Comparator in order to customize what the "max value" is. However, it seems that the libgdx Array<T> class has a method to do just what you want, without having to make a sorted copy of the array.

This solution saves you code, memory and time if you only need one value from the sorted array: the minimum, maximum, whatever. If you need more than one, it is likely that sorting the array will be faster.

The method I'm talking about is Array.selectRanked, which returns the nth element according to the provided Comparator. There is another method selectRankedIndex which returns the index of that element instead of the object itself. You could use it like this:

// If Pawn implements Comparable<Pawn>:
Pawn minVal = arr.selectRanked(Comparator.naturalOrder(), 1);
Pawn maxVal = arr.selectRanked(Comparator.naturalOrder(), arr.size);

// If it does not implement Comparable, you need to provide a Comparator<Pawn>:
// Assuming Pawn has an "int getValue()" method that we want to compare:
Pawn minVal = arr.selectRanked(Comparator.comparingInt(Pawn::getValue), 1);
// You could also write your own implementation directly:
Comparator<Pawn> comp = (a,b) -> /* your int-returning logic here */;
Pawn minVal = arr.selectRanked(comp, 1);

Upvotes: 1

Javier Mart&#237;n
Javier Mart&#237;n

Reputation: 2605

If your problem is with Java arrays (the syntax is Pawn[]) then you have methods in class java.util.Arrays for many different operations on them. What you are asking for could be accomplished with:

Pawn[] newArr = Arrays.copyOf(oldArr, oldArr.length);

Or, since array classes implement Cloneable, also with:

Pawn[] newArr = (Pawn[]) oldArr.clone(); // I don't remember if the cast is necessary

Note that both of these provide shallow copies, that is, the arrays are independent of each other (you can sort one and the indexes in the other are unaffected) but their contents are not.

EDIT: it has been kindly pointed out to me that your Array<T> is actually a class in libgdx. Looking at the documentation, then, you could simply use the constructor taking another instance of Array to create your shallow copy, since the doc says that the new instance will have the same type of backing array (not the same instance). For example:

Array<T> newArr = new Array<>(oldArr); // oldArr can be either Array<? extends T> or T[]

Upvotes: 1

tjago
tjago

Reputation: 174

It seems you have a java related problem. To help you with sorting In java object programming there exist concept of method overriding and interfaces.

Special interface for sorting is Comparator, you can either put him inline in method like this.

Collections.sort(pawns ,new Comparator<Student>(){
                 public int compare(Pawn1 p1,Pawn2 p2){
                       // Write your logic here.
                       //ie.:
                       return p1.score - p2.score; 
                       //or for different order
                       return p2.score - p1.score;
                 }});

if this comparator return value == 0 means the value are equal; if value < 0 means p1 is bigger than p2, therefore swap them.

Or put him inside your Object class like:

Class Pawn implements Comparator {
       private String name;
       private Position[][] posXY;
       private int value;
       ....
       Pawn() { ... }
       ...
       public int compare(Pawn1 p1,Pawn2 p2){
                       return p1.value- p2.value; 
       }

}

then in your code you can call as you originally intended:

pawns.sort();
Pawn best = pawns.get(0);

and as expected you should get an maximum value Pawn from ArrayList.

The above code is just sample and requires tunning. But You should get an good overview now that Java has no idea how to sort Objects defined by a programmer unless he implements the Comparator logic for Collection sorting.

for external reference I suggest running a simple example on tutorialpoint

Upvotes: 0

Related Questions