【问题标题】:How to get and index of an array object如何获取和索引数组对象
【发布时间】:2019-04-15 11:56:47
【问题描述】:

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

我尝试使用 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);
                } 
            }
        } 

【问题讨论】:

  • 您缺少分号并检查 Mince 是否有一个名为 indexOf 的方法
  • 什么是 indexOf(hra.Mince) 在你的 sn-p 中没有那行
  • 您的首要目标应该始终是让代码编译时没有任何错误。

标签: java arrays indexing


【解决方案1】:

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 
}

【讨论】:

    【解决方案2】:

    首先,最好直接循环遍历索引:

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

    我看到你的数组不完整,所以你应该检查空值:

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

    最后,您可以考虑为您的应用程序使用 Java 集合。我建议您使用 java.util.Map 因为我假设 herniPlan 中的索引除了索引之外还有特殊的含义。使用 java.util.Map 您可以将这些值映射到特定的 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);
                } 
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用index = Arrays.asList(herniPlan).indexOf(m)

      Arrays.asList(herniPlan) 将数组转换为 ArrayList&lt;Mince&gt;

      然后使用ArrayList类的ArrayList.indexOf()方法,返回对象在ArrayList中的位置。

      参考:https://www.tutorialspoint.com/java/util/arraylist_indexof.htm

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-23
        • 2020-05-04
        • 2012-02-21
        • 1970-01-01
        • 2018-04-25
        • 2014-11-30
        • 1970-01-01
        • 2017-06-16
        相关资源
        最近更新 更多