简体   繁体   中英

How do I convert an object from an ArrayList() or ArrayList<String> to ArrayList<Integer>?

So, in my code, I read in the .txt file to a single, unspecified ArrayList, but it is not converting back to an int or Integer when I try to parse it back out. The text file looks like this:
Name 1
Number 1
Number 2
Number 3
Name 2
etc.
I am trying to make Number 1 increment up when Name 1 is selected from a list.
students is the initial ArrayList (tried both ArrayList and ArrayList())
kb is Scanner
players is ArrayList
totalgames is ArrayList that tries to rewrite students(k+1)

System.out.println(students.get(k));  
if(kb.nextInt()!=0)  
{  
players.add(k);  
totalgames.add((Integer)students.get(k+1));  
}  

But it comes up with an incompatible types error. Is there a way to make this error go away without completely changing all the ArrayLists?

ArrayList students=new ArrayList();
        int num=0;
        while (qbrdr.hasNext()&&num<loop)
        {
            students.add(qbrdr.nextLine());
            students.add(new Integer(qbrdr.nextInt()));
            students.add(new Double(qbrdr.nextDouble()));
            students.add(new Double(qbrdr.nextDouble()));
            num+=4;
        }  

During the initialization, I just needed it to record them as int or double not Object or String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM