3

I'm trying to convert a list to an array, but I get an error and I can't figure out why. I'm taking the current hour and in a for loop I get the rest of the hours of today and put it in a list. When i try to change it to an array I get an error. I try to do this because later I use that array in a 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 to array which can be used in the combobox
    String[] dropdownElements = times.toArray();

    String[] a = {"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 to 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 i = hour; i <=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 get the following error:

Frame.java:21: error: cannot find symbol  
String[] dropdownElements = times.toArray();  
                                 ^  
    symbol:   method toArray()  
    location: variable times of type List

How do I change the list I get back from the method createDropdown to an array and why is what I did wrong?

1 Answer 1

5

Since you have imported import java.awt.*;, the List used is java.awt.List. You need to add an import - java.util.List.

And please don't use raw type List. It's not recommended in newer code to use raw types. Of course you couldn't have noticed it, because java.awt.List is a non-generic class. So even compiler wouldn't have given you a warning message.

You should use parameterized type - List<String> in your case. Modify your method as:

private List<String> createDropdown(int hour){
    List<String> availableHours = new ArrayList<String>();
    ...
}

You also need to add import java.util.ArrayList.

Sign up to request clarification or add additional context in comments.

4 Comments

your solution works very well, but I don't understand why I have to put <String> behind it. also what is the difference between util and awt list. ps. I'm a beginner
@GertKommer <String> represent an actual type parameter. You need to put it because java.util.List is generic type. You can learn more on generics from docs.oracle.com/javase/tutorial/java/generics
@GertKommer Or for a quick basic introduction, I've written a blog post - rjcodeblog.wordpress.com/2013/09/28/… . That will give you a quick start.
the java.util package version of List is for simply holding a collection of items, for any purpose -- not specifically for displaying in a user interface as a widget. The java.awt version is specifically a "widget" for displaying a list of items in a user interface. I imagine from your variable names that you were purposely using the java.awt version. But that type of List does not have a toArray method defined in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.