Hello,

Can somebody help me with the problem I have? Anyways, I have a ArrayList that consists of Arraylist that consists of 3 elements. [[a1, 11, x], [a2, 12, ], [a3, 13, ], [a4, 14, o]]. The 0th and second element are both strings, the first one an integrer.

At the moment I need to tage the first element and save it to a variable.

int a = (int)(((ArrayList) array.get(randomLocation)).get(1));

It sends an error message

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from Object to int.

But when I try to print it out, everything works perfectly

System.out.println(((ArrayList) array.get(randomLocation)).get(1));

Thanks in advance!

Edited 4 Years Ago by Kert: n/a

can u post the whole code or the area where you declare the array list "array"

This is the class where I create the ArrayList

import java.util.*;
public class Ruudustik{
	static Ruudud a1 = new Ruudud("x", 11);
	static Ruudud a2 = new Ruudud(" ",12);
	static Ruudud a3 = new Ruudud(" ", 13);
	static Ruudud a4 = new Ruudud("o", 14);
	static Ruudud b1 = new Ruudud("x", 21);
	static Ruudud b2 = new Ruudud(" ",22);
	static Ruudud b3 = new Ruudud(" ", 23);
	static Ruudud b4 = new Ruudud("o", 24);
	static Ruudud c1 = new Ruudud("x", 31);
	static Ruudud c2 = new Ruudud(" ",32);
	static Ruudud c3 = new Ruudud(" ", 33);
	static Ruudud c4 = new Ruudud("o", 34);
	static Ruudud d1 = new Ruudud("x", 41);
	static Ruudud d2 = new Ruudud(" ",42);
	static Ruudud d3 = new Ruudud(" ", 43);
	static Ruudud d4 = new Ruudud("o", 44);

	public static ArrayList Jada(){
		ArrayList RuudustikJada = new ArrayList();
		//a1
		ArrayList a_yks = new ArrayList();
		a_yks.add("a1");
		a_yks.add(a1.koordinaat);
		a_yks.add(a1.nupp);
		RuudustikJada.add(a_yks);
		//a2
		ArrayList a_kaks = new ArrayList();
		a_kaks.add("a2");
		a_kaks.add(a2.koordinaat);
		a_kaks.add(a2.nupp);
		RuudustikJada.add(a_kaks);
		//a3
		ArrayList a_kolm = new ArrayList();
		a_kolm.add("a3");
		a_kolm.add(a3.koordinaat);
		a_kolm.add(a3.nupp);
		RuudustikJada.add(a_kolm);
		//a4
		ArrayList a_neli = new ArrayList();
		a_neli.add("a4");
		a_neli.add(a4.koordinaat);
		a_neli.add(a4.nupp);
		RuudustikJada.add(a_neli);
		//b1
		ArrayList b_yks = new ArrayList();
		b_yks.add("b1");
		b_yks.add(b1.koordinaat);
		b_yks.add(b1.nupp);
		RuudustikJada.add(b_yks);
		//b2
		ArrayList b_kaks = new ArrayList();
		b_kaks.add("b2");
		b_kaks.add(b2.koordinaat);
		b_kaks.add(b2.nupp);
		RuudustikJada.add(b_kaks);
		//b3
		ArrayList b_kolm = new ArrayList();
		b_kolm.add("b3");
		b_kolm.add(b3.koordinaat);
		b_kolm.add(b3.nupp);
		RuudustikJada.add(b_kolm);
		//b4
		ArrayList b_neli = new ArrayList();
		b_neli.add("b4");
		b_neli.add(b4.koordinaat);
		b_neli.add(b4.nupp);
		RuudustikJada.add(b_neli);
		//c1
		ArrayList c_yks = new ArrayList();
		c_yks.add("c1");
		c_yks.add(c1.koordinaat);
		c_yks.add(c1.nupp);
		RuudustikJada.add(c_yks);
		//c2
		ArrayList c_kaks = new ArrayList();
		c_kaks.add("c2");
		c_kaks.add(c2.koordinaat);
		c_kaks.add(c2.nupp);
		RuudustikJada.add(c_kaks);
		//c3
		ArrayList c_kolm = new ArrayList();
		c_kolm.add("c3");
		c_kolm.add(c3.koordinaat);
		c_kolm.add(c3.nupp);
		RuudustikJada.add(c_kolm);
		//c4
		ArrayList c_neli = new ArrayList();
		c_neli.add("c4");
		c_neli.add(c4.koordinaat);
		c_neli.add(c4.nupp);
		RuudustikJada.add(c_neli);
		//d1
		ArrayList d_yks = new ArrayList();
		d_yks.add("d1");
		d_yks.add(d1.koordinaat);
		d_yks.add(d1.nupp);
		RuudustikJada.add(d_yks);
		//d2
		ArrayList d_kaks = new ArrayList();
		d_kaks.add("d2");
		d_kaks.add(d2.koordinaat);
		d_kaks.add(d2.nupp);
		RuudustikJada.add(d_kaks);
		//d3
		ArrayList d_kolm = new ArrayList();
		d_kolm.add("d3");
		d_kolm.add(d3.koordinaat);
		d_kolm.add(d3.nupp);
		RuudustikJada.add(d_kolm);
		//d4
		ArrayList d_neli = new ArrayList();
		d_neli.add("d4");
		d_neli.add(d4.koordinaat);
		d_neli.add(d4.nupp);
		RuudustikJada.add(d_neli);
		return RuudustikJada;

	}
}

how is this:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Object to int.

not clear? you are trying to convert an Object to a primitive dataType, of course that doesn't work.

can u post the whole code or the area where you declare the array list "array"

Sorry, I forgot to change the names to "array".

import java.util.*;
public class Ruudustik{
	static Ruudud a1 = new Ruudud("x", 11);
	static Ruudud a2 = new Ruudud(" ",12);
	static Ruudud a3 = new Ruudud(" ", 13);
	static Ruudud a4 = new Ruudud("o", 14);
	static Ruudud b1 = new Ruudud("x", 21);
	static Ruudud b2 = new Ruudud(" ",22);
	static Ruudud b3 = new Ruudud(" ", 23);
	static Ruudud b4 = new Ruudud("o", 24);
	static Ruudud c1 = new Ruudud("x", 31);
	static Ruudud c2 = new Ruudud(" ",32);
	static Ruudud c3 = new Ruudud(" ", 33);
	static Ruudud c4 = new Ruudud("o", 34);
	static Ruudud d1 = new Ruudud("x", 41);
	static Ruudud d2 = new Ruudud(" ",42);
	static Ruudud d3 = new Ruudud(" ", 43);
	static Ruudud d4 = new Ruudud("o", 44);
 
	public static ArrayList Jada(){
		ArrayList array = new ArrayList();
		//a1
		ArrayList a_yks = new ArrayList();
		a_yks.add("a1");
		a_yks.add(a1.koordinaat);
		a_yks.add(a1.nupp);
		arraya.add(a_yks);
		//a2
		ArrayList a_kaks = new ArrayList();
		a_kaks.add("a2");
		a_kaks.add(a2.koordinaat);
		a_kaks.add(a2.nupp);
		array.add(a_kaks);
		//a3
		ArrayList a_kolm = new ArrayList();
		a_kolm.add("a3");
		a_kolm.add(a3.koordinaat);
		a_kolm.add(a3.nupp);
		array.add(a_kolm);
		//a4
		ArrayList a_neli = new ArrayList();
		a_neli.add("a4");
		a_neli.add(a4.koordinaat);
		a_neli.add(a4.nupp);
		array.add(a_neli);
		//b1
		ArrayList b_yks = new ArrayList();
		b_yks.add("b1");
		b_yks.add(b1.koordinaat);
		b_yks.add(b1.nupp);
		array.add(b_yks);
		//b2
		ArrayList b_kaks = new ArrayList();
		b_kaks.add("b2");
		b_kaks.add(b2.koordinaat);
		b_kaks.add(b2.nupp);
		array.add(b_kaks);
		//b3
		ArrayList b_kolm = new ArrayList();
		b_kolm.add("b3");
		b_kolm.add(b3.koordinaat);
		b_kolm.add(b3.nupp);
		array.add(b_kolm);
		//b4
		ArrayList b_neli = new ArrayList();
		b_neli.add("b4");
		b_neli.add(b4.koordinaat);
		b_neli.add(b4.nupp);
		array.add(b_neli);
		//c1
		ArrayList c_yks = new ArrayList();
		c_yks.add("c1");
		c_yks.add(c1.koordinaat);
		c_yks.add(c1.nupp);
		array.add(c_yks);
		//c2
		ArrayList c_kaks = new ArrayList();
		c_kaks.add("c2");
		c_kaks.add(c2.koordinaat);
		c_kaks.add(c2.nupp);
		array.add(c_kaks);
		//c3
		ArrayList c_kolm = new ArrayList();
		c_kolm.add("c3");
		c_kolm.add(c3.koordinaat);
		c_kolm.add(c3.nupp);
		array.add(c_kolm);
		//c4
		ArrayList c_neli = new ArrayList();
		c_neli.add("c4");
		c_neli.add(c4.koordinaat);
		c_neli.add(c4.nupp);
		array.add(c_neli);
		//d1
		ArrayList d_yks = new ArrayList();
		d_yks.add("d1");
		d_yks.add(d1.koordinaat);
		d_yks.add(d1.nupp);
		array.add(d_yks);
		//d2
		ArrayList d_kaks = new ArrayList();
		d_kaks.add("d2");
		d_kaks.add(d2.koordinaat);
		d_kaks.add(d2.nupp);
		array.add(d_kaks);
		//d3
		ArrayList d_kolm = new ArrayList();
		d_kolm.add("d3");
		d_kolm.add(d3.koordinaat);
		d_kolm.add(d3.nupp);
		array.add(d_kolm);
		//d4
		ArrayList d_neli = new ArrayList();
		d_neli.add("d4");
		d_neli.add(d4.koordinaat);
		d_neli.add(d4.nupp);
		array.add(d_neli);
		return array;
 
	}
}

But thanks a lot, Jaggs! It worked. What is the logic behind this step? It isn't the same as just int?

Integer is the wrapper class for an int.

it provides you with the ability to treat the values that you can have in your int (primitive) as Objects. you can store them in (and get them from) Collections and Lists, use .equals to compare them, ....

Thanks, but why I can't use the "==" to compare an int and an Integrer. I'm using it at the moment and everything seems to be working...

Thanks, but why I can't use the "==" to compare an int and an Integrer. I'm using it at the moment and everything seems to be working...

In object oriented languages such as Java, the 'int' type is a primitive , whereas the 'Integer' type is an object.
Objects provide facilities for polymorphism, are passed by reference, and are allocated from the heap. Conversely, primitives are passed by value and are allocated from the stack.
[edit]maybe this will help:http://illegalargumentexception.blogspot.com/2008/08/java-int-versus-integer.html

Edited 4 Years Ago by DavidKroukamp: n/a

This article has been dead for over six months. Start a new discussion instead.