I wanna make an apps that need to convert ArrayList<String>[] to ArrayList<Integer>[] , I also used this :

ArrayList<String>[] strArrayList;
int ArrayRes = (int) strArrayList[];

but this code get me an error , anybody can help me ?

Any suggestion would be appreciate

link|improve this question

77% accept rate
How do you wish to convert Array of an ArrayList of String to a single int? Question is vague. – HashimR Oct 10 '11 at 5:19
Ali Ghanei, do you want to parse your string arraylist into integer arraylist? Simply are you asking that how to convert all string value of your arraylist into int value? – Pankaj Kumar Oct 10 '11 at 5:38
1  
@HashimR A: Question is not vague by the way it was very clear , I just asked my problem. – Mr.James Oct 10 '11 at 5:43
1  
@pankaj : yes . – Mr.James Oct 10 '11 at 5:43
Okay first thing, why don't you make simple ArrayList<String> ? And how do you want to convert whole ArrayList to a single int? Do you want to concatenate all Strings? you didn't mention anything so your question seems incomplete. No offense. :) – HashimR Oct 10 '11 at 5:47
show 2 more comments
feedback

closed as not a real question by BalusC, Brian Roach, dacwe, Reno, Graviton Oct 10 '11 at 7:13

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. See the FAQ for guidance on how to improve it.

4 Answers

How about this one

   import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class sample7 
    {
        public static void main(String[] args)
        {

            ArrayList<String> strArrayList = new ArrayList<String>();
            strArrayList.add("1");
            strArrayList.add("11");
            strArrayList.add("111");
            strArrayList.add("12343");
            strArrayList.add("18475");
            List<Integer> newList = new ArrayList<Integer>(strArrayList.size()) ;
            for (String myInt : strArrayList) 
            { 
              newList.add(Integer.valueOf(myInt)); 
            }
            System.out.println(newList);
        }

    }
link|improve this answer
feedback

Iterator on the list and parse them into Integers:

(WARN: not tested)

ArrayList<String> strArrayList;
int[] ArrayRes = new int[strArrayList.size()];

int i = 0;
for (String s : strArrayList)
{
    ArrayRes[i++] = Integer.parseInt(s);
}

You can then convert them to a single int value based on how you wish to concatenate them.

link|improve this answer
Its asking for a conversion of ArrayList to int not int[] – Umer Hayat Oct 10 '11 at 5:05
feedback

You can cast list of strings to ints like this:

ArrayList<int> numbers = new ArrayList<int>();

for(int i = 0; i < strArrayList.size(); i++) {
   numbers.add(Integer.parseInt(strArrayList.get(i)));   
}
link|improve this answer
strArrayList[i] would give you an ArrayList type, which is not a valid argument for Integer.parseInt – bdares Oct 10 '11 at 5:07
feedback

You're trying to cast an array of ArrayList objects to an int. Of course you get an error.

First of all, you want a plain old ArrayList, not an array of ArrayLists.

Second, you use Integer.parseInt() to turn String objects into ints. int is a primitive type, not a class type, and certainly not a superclass of String.

link|improve this answer
feedback

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