Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I encountered a problem when I tried to run the following code:

ArrayList paretoSet=new ArrayList();   // contains a collection of ArrayList
ArrayList<Integer> toPass=new ArrayList<Integer>();
int[] fParetoSet=new int[ParetoSet.size()];
int[] gParetoSet=new int[ParetoSet.size()];

for (int i=0;i<paretoSet.size();i++){
        toPass.clear();
        toPass.add((Integer)paretoSet.get(i));
        int [] totake=calculate(toPass);
        fParetoSet[i]=totake[0];
        gParetoSet[i]=totake[1];       
    }

` where claculate(ArrayList x) is a method that takes an integer arraylist and returns an integer array. I can not make Paretoset an integer arraylist as it creates problem in other parts of my program. I encountered an exception in the line toPass.add((Integer)paretoSet.get(i));
as java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer How should I fix this problem?
Thanks in advance

share|improve this question
2  
Also, please use Java coding conventions. PareToSet should start with a lower case letter; I thought you were calling a static method at first. –  markspace Aug 12 '14 at 3:27
    
You haven't shown us what goes into ParetoSet. I assume it's ArrayLists, but show us to confirm. –  Sotirios Delimanolis Aug 12 '14 at 3:30
    
So the type of ParetoSet is ArrayList<ArrayList<Integer>>, correct? –  Steve McKay Aug 12 '14 at 3:35
    
((Integer)ParetoSet.get(i)), you cast a arraylist to int. toPass.add(ParetoSet.get(i)) may be what you want. –  criszhao Aug 12 '14 at 3:39
    
are you sure you are getting an error? Because I just copy paste and run your code. It works without any issues. One more thing you don't have to cast ParetoSet.size() to Integer –  Thusitha Thilina Dayaratne Aug 12 '14 at 3:41

2 Answers 2

up vote 1 down vote accepted

If ParetoSet is a collection of ArrayList, then the call ParetoSet.get(i) will return the ArrayList at index i. As the error says, an ArrayList is not a a type of Integer and cannot be cast to one.

Other points of interest:

  • your variable should be camel case: pareToSet
  • auto-boxing means the cast in your for loop has been unnecessary since JDK5
  • Paretoset has been declared with a raw type
  • type inference makes new ArrayList<Integer>() redundant since JDK7

EDIT

  • your variable should be camel case: paretoSet, as per Jim's comments

EDIT

An ArrayList is neither conceptually, or actually an Integer. If you say the sentence 'A list is a type of integer' it doesn't make sense. If we check the javadoc for ArrayList we can see that its inheritance tree is:

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

So we can say for example that ArrayList is a type of AbstractList, or AbstractCollection, but not an Integer as Integer is not part of its lineage.

share|improve this answer
    
Could be "Pareto Principle"... –  Jim Garrison Aug 12 '14 at 3:58
    
Yes, you got it correctly but this is what my program requires. I need to pass an arraylist of integer type to method calculate() and I can not make paretoSet an integer arraylist as it creates problem in other methods of the program. Is there any other way of doing that? –  Neha Aug 12 '14 at 8:41
    
You are missing the point, toPass is an ArrayList of Integer and can, presumably, be passed to your calculate() method. Your problem is that you have said that toPass can only accept Integers and then you are trying to add an ArrayList to it. –  Romski Aug 12 '14 at 23:36
    
If this answers your question, then please accept it –  Romski Aug 13 '14 at 6:50
    
Please note that I tried to add (Integer)paretoSet.get(i) to toPass and this is where the problem occurs. I tried to cast an arraylist at index i in paretoSet to integer type and then added it to toPass but this casting is not allowed!!! and my problem remains as it is. –  Neha Aug 13 '14 at 10:09

your program having typecasting issue on below lines. your are trying to add typecast list of arraylist to Integer arryalist. this wrong. if you want add the arraylist objects to some other arryalist. Please use generics as object.

ArrayList ParetoSet=new ArrayList();   // contains a collection of ArrayList
toPass.add((Integer)ParetoSet.get(i));

This should be like below:

ArrayList<ArrayList> ParetoSet=new ArrayList<ArrayList>();  
ArrayList<ArrayList> toPass=new ArrayList<ArrayList>();  
toPass.add(ParetoSet.get(i));

Then you should change code bit to fit your further your logics

share|improve this answer
    
thanks..... but this did not work. –  Neha Aug 12 '14 at 8:31
    
Please put complete code as what you are trying to achieve as sample program and I may try to find the real issue.. –  Prabhu Aug 12 '14 at 13:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.