AterCZ
AterCZ

Reputation: 17

How to get and index of an array object

I'm going through an array and trying to get an index of an object meeting some criteria. I can't figure out a way to do that.

I've tried using herniPlan.indexOf(m), I'm getting "Cannot find symbol - method indexOf(hra.Mince)

public class MojeHra implements IHra {
private Mince[] herniPlan;
int index;

    public MojeHra()
    {
        herniPlan = new Mince[20];

        herniPlan[0] = Mince.LITECOIN;
        herniPlan[3] = Mince.LITECOIN;
        herniPlan[4] = Mince.BITCOIN;
        herniPlan[8] = Mince.LITECOIN;

        hracVyhral = false;
        hraSkoncila = false;
    }

    public Tah tahPocitace()
    {


        for(Mince m : herniPlan) {
            if(m.equals(Mince.LITECOIN) || m.equals(Mince.BITCOIN)){
                index = herniPlan.indexOf(m)
                    Tah tah = new Tah(index, 19);
                } 
            }
        } 

Upvotes: 0

Views: 97

Answers (3)

Hien Nguyen
Hien Nguyen

Reputation: 18973

You can use index = Arrays.asList(herniPlan).indexOf(m)

Arrays.asList(herniPlan) will cast array to ArrayList<Mince>

And then it use method ArrayList.indexOf() of ArrayList class, it will return position of object in ArrayList.

Refer: https://www.tutorialspoint.com/java/util/arraylist_indexof.htm

Upvotes: 0

Michal Horvath
Michal Horvath

Reputation: 183

Firstly, it would be better to loop through index directly:

for(int i = 0; i < herniPlan.length; i++) {
    if(herniPlan[i].equals(Mince.LITECOIN) || herniPlan[i].equals(Mince.BITCOIN)){
        index = i;
    }
}

I see that you array is incomplete, so you should check for nulls:

for(int i = 0; i < herniPlan.length; i++) {
    if(herniPlan[i] != null) {
        if(herniPlan[i].equals(Mince.LITECOIN) 
                || herniPlan[i].equals(Mince.BITCOIN)){
            index = i;
        }
    }
}

Lastly, you might consider using Java Collections for your applications. I would suggest you to use java.util.Map since I assume indexes in herniPlan have special meaning beyond just indexing. With java.util.Map you can map those values to specific Mince.

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MojeHra implements IHra {
    private Map<Integer, Mince> herniPlan;
    int index;

    public MojeHra() {
        herniPlan = new HashMap<>();

        herniPlan.put(0, Mince.LITECOIN);
        herniPlan.put(3, Mince.LITECOIN);
        herniPlan.put(4, Mince.BITCOIN);
        herniPlan.put(8, Mince.LITECOIN);

        hracVyhral = false;
        hraSkoncila = false;
    }

    public Tah tahPocitace()
    {
        for(Entry<Integer, Mince> entry : herniPlan.entrySet()) {
            if(entry.getValue().equals(Mince.LITECOIN) 
                    || entry.getValue().equals(Mince.BITCOIN)){
                index = entry.getKey();
                    Tah tah = new Tah(index, 19);
            } 
        }
    }
}

Upvotes: 1

Andrew
Andrew

Reputation: 49656

The enhanced for statement (for(... : ...)) doesn't suggest indexing an array. You need the basic for statement (for(...; ...; ...)).

for (int i = 0; i < herniPlan.length; ++i) {
    Mince  m = herniPlan[i];
    // i is your index 
}

Upvotes: 2

Related Questions