假设列表类型为int-java

假设列表类型为int-java,java,arrays,list,types,casting,Java,Arrays,List,Types,Casting,我有一个返回值列表的函数。我想将该列表中的值用作另一个函数中的参数 private static List test(){ List myList; mylist.add(1); return myList; }; 现在,问题来了。当我说 lst = test(); myFunction(lst.get(1)); lst.get(1)是类型对象。但是myFunction需要一个int。我已经尝试将它转换成很多东西。当我说(int)lst.get(1)我的编译器返回此错

我有一个返回值列表的函数。我想将该列表中的值用作另一个函数中的参数

private static List test(){
    List myList;
    mylist.add(1);
    return myList;
};
现在,问题来了。当我说

lst = test();
myFunction(lst.get(1));
lst.get(1)
是类型对象。但是
myFunction
需要一个int。我已经尝试将它转换成很多东西。当我说
(int)lst.get(1)我的编译器返回此错误:

C:\Users\...\workspace\...\....txt
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at ///.///.Encode(///.java:73)
    at ///.///.main(///.java:25)
当我没有强制转换的时候,我得到了这个红色下划线和错误:

The method ENCODEScrambleNum(int, int, int, int, String) in the type kriptik is not applicable for the arguments (Object, Object, Object, Object, String)
方法签名:

ENCODEScrambleNum(int, int, int, int, String)
称之为:

ENCODEScrambleNum(key.get(0), key.get(1), key.get(2), key.get(3), str);
有没有一种方法可以让我事先告诉计算机列表类型是int


谢谢。

哦,是的,你能做到。只需像这样声明列表的类型

private static List<Integer> test(){
    //List<Integer> myList; // list is not initialized yet(NPE is waiting for you)
    List<Integer> myList = new ArrayList<Integer>(); // List initialized
    mylist.add(1);
    return myList;
} // Why was a semi-colon here?
私有静态列表测试(){
//列表myList;//列表尚未初始化(NPE正在等待您)
List myList=new ArrayList();//列表已初始化
添加(1);
返回myList;
}//为什么这里有分号?

当您尝试将
list.get(1)
作为
int
参数发送时,它将自动装箱。所以你不必为此担心。

哦,是的,你可以做到。只需像这样声明列表的类型

private static List<Integer> test(){
    //List<Integer> myList; // list is not initialized yet(NPE is waiting for you)
    List<Integer> myList = new ArrayList<Integer>(); // List initialized
    mylist.add(1);
    return myList;
} // Why was a semi-colon here?
private static List test(){
    List myList;
    mylist.add(1);    //Here the value 1 is added at zeroth index.
    return myList;
}
私有静态列表测试(){
//列表myList;//列表尚未初始化(NPE正在等待您)
List myList=new ArrayList();//列表已初始化
添加(1);
返回myList;
}//为什么这里有分号?
当您尝试将
list.get(1)
作为
int
参数发送时,它将自动装箱。所以你不用担心

private static List test(){
    List myList;
    mylist.add(1);    //Here the value 1 is added at zeroth index.
    return myList;
}
将代码替换为

lst = test();
myFunction(lst.get(0));  //Retrieves the value at zeroth index.
而不是

lst = test();
myFunction(lst.get(1)); //Retrieves the value at first index
因为
列表
索引从
0
开始,而不是从
1
开始

将代码替换为

lst = test();
myFunction(lst.get(0));  //Retrieves the value at zeroth index.
而不是

lst = test();
myFunction(lst.get(1)); //Retrieves the value at first index

因为
List
索引从
0
开始,而不是从
1
开始,所以我同意R.J,如果将列表类型指定为整数并使用get()函数,它将始终返回整数。我尝试了下面的代码,希望能有所帮助:

private static List<Integer> test(){

List<Integer> myList = new ArrayList<Integer>(); 
for(int i=0; i<4; i++){ myList.add(i+10);} //included a for loop just to put something in the list
return myList;
}

private static String ENCODEScrambleNum(Integer get, Integer get0, Integer get1, Integer get2, String in_here) {
     return "I am " + in_here + " with list items-" + get + get0 + get1 + get2; //Dummy logic
}
私有静态列表测试(){
List myList=new ArrayList();

对于(int i=0;i我同意R.J,如果您将列表的类型指定为integer并使用get()函数,它将始终返回整数。我尝试了以下代码,希望对您有所帮助:

private static List<Integer> test(){

List<Integer> myList = new ArrayList<Integer>(); 
for(int i=0; i<4; i++){ myList.add(i+10);} //included a for loop just to put something in the list
return myList;
}

private static String ENCODEScrambleNum(Integer get, Integer get0, Integer get1, Integer get2, String in_here) {
     return "I am " + in_here + " with list items-" + get + get0 + get1 + get2; //Dummy logic
}
私有静态列表测试(){
List myList=new ArrayList();

对于(int i=0;iHmm…仍然得到相同的编译错误。仍然表现得像对象一样。您可以发布
myFunction()
方法的签名吗?您如何准确地使用
列表
项作为参数来调用它?String numText=ENCODEScrambleNum(key.get(1),key.get(2),key.get(3),key.get(4));这是假设key=test()请编辑您的问题,并将方法签名和调用一起发布到那里。在此处发布代码将使注释变得非常笨拙。嗯……仍然会出现相同的编译错误。它们仍然像对象一样。您能发布
myFunction()的签名吗
方法,以及您如何使用
列表
项作为参数来调用它?字符串numText=ENCODEScrambleNum(key.get(1),key.get(2),key.get(3),key.get(4));这是假设key=test()请编辑您的问题,并将方法签名与调用一起发布到此处。在此处发布代码将使注释变得非常笨拙。尝试了该操作,但没有帮助。谢谢。尝试了该操作,但没有帮助。谢谢。它仍然像
key.get(1);
是一个对象。它不会将其识别为整数。它的行为仍然像
键。get(1);
是一个对象。它不会将其识别为整数。