1

I'm learning Java step by step. Now I know up to arrays, and next is 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). So, first I'm trying to convert an int[] into a Collection. But I get an error in the foreach loop...

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:

required `String` but found `Object`

If I define the arry as String and store Strings, it wouldn't be a error, but for int it's not working..

ccjmne
  • 9,333
  • 3
  • 47
  • 62
user562
  • 88
  • 1
  • 11

4 Answers4

0

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[]. And, two you have a raw type (so your Integer elements are being treated as Object elements). 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[] arry=new int[siz];

to

Integer[] arry=new Integer[siz];
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 3
    Now you will get this compiler error at: `Arrays.asList(arry)`. Type mismatch: cannot convert from List to List – kai Jul 02 '14 at 06:16
  • thank you.. changing it to Integer arry from int arry works out and after making it as list, Collections.reverse(arrycoll) works fine.. But what is the diff b/w use of Integer and int.. From the start I thought integer is expansion and int is keyword for defining datatype in program.. @Elliott Frisch – user562 Jul 02 '14 at 06:47
  • @krnaveen14 No. `int` is a [primitive type](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). `Integer` is a wrapper class. The primitive types are a performance optimization. Also, the `Collection`(s) require Object instances. – Elliott Frisch Jul 02 '14 at 06:51
  • The main difference is that Integers can be used as subclasses of Object, for example be stored in a list or map as generic parameters. The "Integer" *class* also provides additional methods for the int. While most of the time this is hidden due to auto-boxing, you can explicitly cast between int to Integer. Also, a major key difference is that Integer can have the value *null*. – EpicPandaForce Jul 02 '14 at 08:52
0

No problem!

  1. Change List to List which will now staticky type the Objects in the list to integers (Badically, they are all treated as Integers)

  2. Replace String with Integer, as now all the objects in the list are of type Integer (not String or anything else)

Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22
0

required String but found 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.

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> arrycoll

and

  1. use Integer instead of String for elem

  2. Replace int[] arry = new int[siz]; with 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);
        }
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
0

For print purpose only, simply try like this; casting as int[]

List arrycoll = Arrays.asList(arry);
for (Object elem : arrycoll) {

    int[] iArr1 = (int[]) elem;     
    for (int i : iArr1) {
        System.out.println(i);
    }
}
Wundwin Born
  • 3,467
  • 19
  • 37