java - method toArray() not working on a list -


i'm trying convert list array, error , can't figure out why. i'm taking current hour , in loop rest of hours of today , put in list. when try change array error. try because later use array in jcombobox

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.gregoriancalendar; import java.util.calendar;  public class frame extends jframe implements actionlistener{     //jframe elements     private jbutton btnsettime;     private jlabel lbltitle;     private jcombobox combotime;      //standard elements     private timer tillpopup, tillshutdown;      calendar calendar = new gregoriancalendar();     int hour = (calendar.get(calendar.hour_of_day));     list times = createdropdown(hour);     // convert arraylist array can used in combobox     string[] dropdownelements = times.toarray();      string[] = {"a","b"};     public frame(){         setlayout(new flowlayout());          //labels         lbltitle = new jlabel("deze applicatie sluit u computer automatisch af om het energieverbruik te verminderen.");          //combobox         combotime = new jcombobox(a);         combotime.setselectedindex(0);          //button         btnsettime = new jbutton("zet afsluittijd");          //timers         //tillpopup = new timer(this);         //tillshutdown = new timer(this);          //add elements frame         add(lbltitle);         add(combotime);         add(btnsettime);          //add actionlisteners         btnsettime.addactionlistener(this);          setsize(500,300);         setvisible(true);         setdefaultcloseoperation(exit_on_close);     }     private list createdropdown(int hour){         list availablehours = new list();         for(int = hour; <=24; i++){             if (i != 24){                 availablehours.add(i + ":00");             }             else if(i == 24){                 availablehours.add("00:00");             }         }         return availablehours;     }     public void actionperformed(actionevent e){         if (e.getsource() == btnsettime){             object popuptime = combotime.getselecteditem();                 system.out.println(popuptime);          }     } } 

i following error:

frame.java:21: error: cannot find symbol   string[] dropdownelements = times.toarray();                                    ^       symbol:   method toarray()       location: variable times of type list 

how change list method createdropdown array , why did wrong?

since have imported import java.awt.*;, list used java.awt.list. need add import - java.util.list.

and please don't use raw type list. it's not recommended in newer code use raw types. of course couldn't have noticed it, because java.awt.list non-generic class. compiler wouldn't have given warning message.

you should use parameterized type - list<string> in case. modify method as:

private list<string> createdropdown(int hour){     list<string> availablehours = new arraylist<string>();     ... } 

you need add import java.util.arraylist.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -