【问题标题】:The type of the expression must be an array type but it resolved to ArrayList<Integer>表达式的类型必须是数组类型,但它解析为 ArrayList<Integer>
【发布时间】:2015-07-12 04:24:38
【问题描述】:

我正在尝试将 ints 放入 Arraylist myList 但 Eclipse 指向错误: The type of the expression must be an array type but it resolved to ArrayList&lt;Integer&gt;

我尝试将原始类型转换为对象类型,但仍然遇到相同的错误。可以帮帮我吗?

import java.util.*;
    public class RemoveDuplicates {

        public static void main(String[] args){

            int[] myArray = {1,1,2,3,4,5,6,7,8,8,9,9,99};
            ArrayList<Integer> myList = new ArrayList();

            for(int i = 0; i < myArray.length; i++){
                for(int j = 1; j < myArray.length; j++){
                    if(myArray[i] == myArray[j]){
                        myList.add(myList[i]);
                    }
                }
            }
        }
    }

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    您无法使用此语法从列表中获取元素。

    myList[i]
    

    仅对数组有效。相反,使用

    myList.get(i);
    

    方法。尽管您打算将myArray[i] 放在那里,但更有可能。

    【讨论】:

    • 哦,我发现了我的错误,并以错误的方式提出了问题。不过谢谢!固定!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2012-04-20
    相关资源
    最近更新 更多