This discussion is archived
8 Replies Latest reply: May 22, 2013 8:19 AM by aksarben RSS

adding  different  data types to collection & how to retrive ?

1008567 Newbie
Currently Being Moderated
Hi all,

public static void main(String[] args) {
                    
                    
          ArrayList ar = new ArrayList();
          ar.add("Sting1");
          ar.add(12);
          ar.add(15.4);
          
          Iterator it = ar.iterator();
          while(it.hasNext()){
               String s =(String)it.next();
          
               System.out.println("vlaues are :::"+s);
          }
               }
============================================
Here I am getting Exception:
Exception in thread "main" vlaues are :::Sting1
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
     at collections.VectorDemo.main(VectorDemo.java:18)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points