[英]Converting int array to collections
I'm learning Java step by step. 我正在逐步学习Java。 Now I know up to arrays, and next is Collections.
现在我最多了解数组,接下来是Collections。
As I'm learning based on projects, I noticed that most of programmers choose to use Collections
rather than plain arrays to eliminate the loops and to make use of the predefined functions (like sort
and much more). 当我基于项目学习时,我注意到大多数程序员选择使用
Collections
而不是普通数组来消除循环并使用预定义的函数(例如sort
等)。 So, first I'm trying to convert an int[]
into a Collection
. 因此,首先,我尝试将
int[]
转换为Collection
。 But I get an error in the foreach
loop... 但是我在
foreach
循环中遇到错误...
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Coll {
public static void main(String[] args) {
Coll obj = new Coll();
obj.op();
}
void op() {
System.out.println("To convert Array to Collections");
Scanner getinp = new Scanner(System.in);
System.out.println("Enter the size of array : ");
int siz = getinp.nextInt();
int[] arry = new int[siz];
System.out.println("Enter the values of elements");
for (int i = 0; i < arry.length; i++) {
System.out.println("arry[" + i + "] : ");
arry[i] = getinp.nextInt();
}
List arrycoll = Arrays.asList(arry);
for (String elem : arrycoll) {
System.out.println(elem);
}
}
}
The error is in the following line: 错误在以下行中:
for(String elem : arrycoll)
Pointing to arrycoll
, it states: 指向
arrycoll
,它指出:
required `String` but found `Object`
If I define the arry
as String
and store String
s, it wouldn't be a error, but for int
it's not working.. 如果我将
arry
定义为String
并存储String
,那不会是一个错误,但是对于int
来说,它是行不通的。
Given this message 鉴于此消息
it states required String but found object
它声明了必需的字符串但找到了对象
You are getting two important pieces of information. 您将获得两个重要的信息。 One, you are using
String
for elements of your int[]
. 第一,您将
String
用于int[]
元素。 And, two you have a raw type (so your Integer elements are being treated as Object elements). 并且,您有两个原始类型(因此您的Integer元素被视为Object元素)。 Don't use Raw Types !
不要使用原始类型 ! I'm pretty sure you wanted something like
我很确定你想要这样的东西
List<Integer> arrycoll = Arrays.asList(arry);
for(Integer elem:arrycoll) { // <-- Integer, not String.
System.out.println(elem);
}
Edit 编辑
Of course to work with a collection, you'll need an object. 当然,要使用集合,您将需要一个对象。
int
has the Integer
wrapper type, so you'll need to change int
具有Integer
包装器类型,因此您需要进行更改
int[] arry=new int[siz];
to 至
Integer[] arry=new Integer[siz];
No problem! 没问题!
Change List to List which will now staticky type the Objects in the list to integers (Badically, they are all treated as Integers) 将列表更改为列表,现在可以将列表中的对象静态键入整数(通常,它们都被视为整数)
Replace String with Integer, as now all the objects in the list are of type Integer (not String or anything else) 用Integer替换String,因为现在列表中的所有对象都是Integer类型的(不是String或其他类型)
required
String
but foundObject
必需的
String
但找到Object
In your for each loop, you are using String as type for element whereas arrycoll is list of raw List references whose type is Object. 在for每个循环中,您使用String作为元素的类型,而arrycoll是原始List引用的列表,其类型为Object。
List arrycoll
You've declared list of raw List references. 您已经声明了原始列表引用的列表。 You have to tell the compiler that they'll be
List<Integer> values
: 您必须告诉编译器它们将是
List<Integer> values
:
List<Integer> arrycoll
and 和
use Integer instead of String for elem 使用Integer代替String作为elem
Replace int[] arry = new int[siz];
替换
int[] arry = new int[siz];
with Integer[] arry = new Integer[siz]; 使用Integer [] arry = new Integer [siz];
Hence code will become: 因此,代码将变为:
Integer[] arry = new Integer[siz];
List<Integer> arrycoll = Arrays.asList(arry);
for (Integer elem : arrycoll) {
System.out.println(elem);
}
For print purpose only, simply try like this; 仅出于打印目的,只需尝试这样; casting as
int[]
转换为
int[]
List arrycoll = Arrays.asList(arry);
for (Object elem : arrycoll) {
int[] iArr1 = (int[]) elem;
for (int i : iArr1) {
System.out.println(i);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.