Home:ALL Converter>Is it possible to add int into the String ArrayList with out Converting int to String in java

Is it possible to add int into the String ArrayList with out Converting int to String in java

Ask Time:2014-02-20T15:24:02         Author:Kishan Bheemajiyani

Json Formatter

i have an ArrayList which contains String elements and i want to add an int into the list but with out Converting that into the String is that possible.

i have tried this and this is working too.

int a1 = 10;
java.util.List list = new ArrayList<String>();
list.add(a1);
System.out.println("List element"+list.get(0));

and but i am wondering this to be happen.

int a1 = 10;
java.util.List<String> list = new ArrayList<String>();
            list.add(a1);
            System.out.println("List element"+list.get(0));

is that possible to do?

Author:Kishan Bheemajiyani,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21900923/is-it-possible-to-add-int-into-the-string-arraylist-with-out-converting-int-to-s
Evgeniy Dorofeev :

try this\n\n list.add(String.valueOf(a1));\n",
2014-02-20T07:27:40
Smutje :

An advantage of a typed list is, that you can assume, that you only have objects of the given type in the list - why would one want to throw this advantage away, if it is completely easy to convert the given type into the type of the list?",
2014-02-20T07:26:20
Ruchira Gayan Ranaweera :

In first case your List is just a raw List. So you can add what ever type to that since List is raw and again that is bad way of use of List in Java..But second case it is String List you have to add Stings there.\n\nNow you have to use following way to add int to List.\n\n int a1 = 10;\n java.util.List<String> list = new ArrayList<String>();\n list.add(String.valueOf(a1)); // now String value of int will add to list\n System.out.println(\"List element\"+list.get(0));\n",
2014-02-20T07:27:01
yy