表达式的类型必须是数组类型,但它解析为 ArrayList<Integer>; [英] The type of the expression must be an array type but it resolved to ArrayList&lt;Integer&gt;

查看:34
本文介绍了表达式的类型必须是数组类型,但它解析为 ArrayList<Integer>;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ints 放入 Arraylist myList 但 Eclipse 指向错误:表达式的类型必须是数组类型但解析为ArrayList

I'm trying to put the intsinto the Arraylist myList but Eclipse is pointing to the error: The type of the expression must be an array type but it resolved to ArrayList<Integer>

我尝试从原始类型转换为对象类型,但仍然遇到相同的错误.请smb帮我解决这个问题吗?

I tried to convert from primitive type into Object type but still getting the same error. COuld smb please help me out with that ?

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

推荐答案

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

You can't get elements from a list using this syntax.

myList[i]

这仅对数组有效.相反,使用

Which is only valid for arrays. Instead, use the

myList.get(i);

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

method. More likely though you meant to put myArray[i] there.

这篇关于表达式的类型必须是数组类型,但它解析为 ArrayList<Integer>;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆