简体   繁体   English

假设列表类型为int java

[英]Assume list type is int java

I have a function that returns a list of values. 我有一个返回值列表的函数。 I want to use the values in that list as parameters in another function. 我想将该列表中的值用作另一个函数中的参数。

private static List test(){
    List myList;
    mylist.add(1);
    return myList;
};

Now here's the catch. 现在是要抓住的地方。 When I say 当我说

lst = test();
myFunction(lst.get(1));

lst.get(1) is type object. lst.get(1)是对象类型。 But myFunction requires an int. 但是myFunction需要一个int。 I've tried casting it into lots of things. 我已经尝试将其转换为很多东西。 When I say (int) lst.get(1); 当我说(int) lst.get(1); my compiler returns this error: 我的编译器返回此错误:

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)

When I just have it with no cast, I get this red underline and error: 当我没有任何转换时,就会出现红色下划线和错误:

The method ENCODEScrambleNum(int, int, int, int, String) in the type kriptik is not applicable for the arguments (Object, Object, Object, Object, String)

Method signature: 方法签名:

ENCODEScrambleNum(int, int, int, int, String)

Calling it: 调用它:

ENCODEScrambleNum(key.get(0), key.get(1), key.get(2), key.get(3), str);

Is there a way that I can tell the computer before hand that the list type will be an int? 有没有办法我可以事先告诉计算机列表类型将是一个int?

Thanks. 谢谢。

Oh yes, you can do that. 哦,是的,你可以做到。 Just by declaring the type of the list like this 只需声明这样的列表类型

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?

When you try to send list.get(1) as an int parameter, it'll be autoboxed. 当您尝试将list.get(1)作为int参数发送时,它将被自动装箱。 So you need not worry about that. 因此,您不必为此担心。

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

Replace your code as 将您的代码替换为

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

instead of, 代替,

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

because the List index starts from 0 and not from 1 . 因为List索引从0开始而不是从1

I agree with RJ, if you specify the type of list as integer and use get() function, it will always return an integer. 我同意RJ,如果您将列表的类型指定为整数并使用get()函数,它将始终返回整数。 I tried below code, hope it helps : 我尝试了下面的代码,希望对您有所帮助:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM