java - 如何获取数组对象及其索引

标签 java arrays indexing

我正在遍历一个数组并尝试获取满足某些条件的对象的索引。我想不出办法做到这一点。

我尝试使用herniPlan.indexOf(m),但收到“找不到符号 - 方法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);
                } 
            }
        } 

最佳答案

The enhanced for statement (for(... : ...)) 不建议对数组建立索引。您需要the basic for statement (for(...; ...; ...))。

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

关于java - 如何获取数组对象及其索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688670/

相关文章:

r - 根据另一列中的过去值标记一列中的行

MYSQL - 如何为 group by/order by/sum/with where 添加索引

java - Quartz Java 在多台机器上运行作业

java - 在 Leiningen 项目的 Google Maven 存储库中找不到 Firebase jar

java - 如何在多个java类中自动修改代码?

java - Spring Advice 不适用于递归 bean

ruby - 将元素插入新数组然后从旧数组中删除,一些元素被忽略

java - 在 Java 中创建泛型数组

mysql - Mysql information_schema可以不与show create table对齐吗?

php - 在 php 和 mysql 中以数组格式获取数据