0

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);
                } 
            }
        } 

3
  • You're missing a semicolon and check if Mince have a method called indexOf Commented Apr 15, 2019 at 11:59
  • What is indexOf(hra.Mince) in your snippet there is not that line Commented Apr 15, 2019 at 11:59
  • 1
    Your first goal should always be to have code that compiles without any errors. Commented Apr 15, 2019 at 12:08

3 Answers 3

2

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 
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
            } 
        }
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.