java - Printing a member from the object returned by itr.next() -


the following code has compilation issues. compilation error attached. please suggest solution same.

code:

import java.util.*;  class test{      int key;      public static void main(string []args){         test obj = new test();         obj.key = 9999;          linkedlist al = new linkedlist();         al.add(obj);          iterator itr = al.iterator();         while(itr.hasnext()){             test temp = new test();             temp = itr.next();             system.out.println(temp.key);         }        } } 

compilation error :

test.java:17: error: incompatible types

temp = itr.next(); ^

required: test

found: object

its.next() returns instance of object , not test

make follows

linkedlist<test> al = new linkedlist<test>(); al.add(obj);  iterator<test> itr = al.iterator(); 

so compiler knows linkedlist a1 can hold test instances while iterating have instances of test


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -