我正在尝试创建一个程序,该计划将获得将要评分的学生数量,然后获得x个成绩并按升序对其进行冒泡排序。
我不知道如何将arraylist中的成绩添加到jlist并输出。 这是我的错误:
Unresolved compilation problem:
The method setListData(Integer[]) in the type JList<Integer> is not applicable for the arguments (Object[])
这是我的代码:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
public class bubbleSwing {
private JFrame frame;
private JTextField inputNoOfGrades;
private JTextField inputGrades;
private String inputStr;
private JLabel tvNoOfGrades;
private JButton btnNoOfGrades;
private JLabel tvGrades;
private JButton btnGrades;
private JButton btnSort;
private JList<Integer> list;
ArrayList<Integer> grades = new ArrayList<Integer>();
private int numOfGrades;
private int inputGradesInt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bubbleSwing bubbleSwing = new bubbleSwing();
bubbleSwing.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bubbleSwing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
tvNoOfGrades = new JLabel("New label");
tvNoOfGrades.setBounds(10, 11, 150, 29);
frame.getContentPane().add(tvNoOfGrades);
tvGrades = new JLabel("New label");
tvGrades.setBounds(170, 11, 150, 29);
frame.getContentPane().add(tvGrades);
inputNoOfGrades = new JTextField();
inputNoOfGrades.setBounds(10, 51, 86, 20);
frame.getContentPane().add(inputNoOfGrades);
inputNoOfGrades.setColumns(10);
inputGrades = new JTextField();
inputGrades.setBounds(158, 51, 86, 20);
frame.getContentPane().add(inputGrades);
inputGrades.setColumns(10);
btnNoOfGrades = new JButton("Submit");
btnNoOfGrades.setBounds(10, 82, 89, 23);
frame.getContentPane().add(btnNoOfGrades);
btnGrades = new JButton("Submit grades");
btnGrades.setBounds(156, 83, 101, 20);
frame.getContentPane().add(btnGrades);
btnSort = new JButton("Sort");
btnSort.setBounds(290, 61, 89, 23);
frame.getContentPane().add(btnSort);
list = new JList<Integer>();
list.setBounds(60, 116, 167, 135);
frame.getContentPane().add(list);
btnNoOfGrades.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getNoOfGrades();
}
});
btnGrades.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addToList();
}
});
btnSort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bubbleSort(grades);
}
});
}
private void addToList() {
inputStr = inputGrades.getText().toString().replaceAll(" ", "");
if (inputStr.isEmpty()) {
tvGrades.setText("no grades added");
} else {
// converts string to integer and checks if item is already
// added
if (grades.size() < numOfGrades) {
inputGradesInt = Integer.parseInt(inputStr);
if (inputGradesInt < 0 || inputGradesInt > 10) {
tvGrades.setText("Enter a grade between 1-10");
} else {
grades.add(inputGradesInt);
list.setListData( grades.toArray());
inputGrades.setText("");
list.notify();
}
} else {
btnSort.setEnabled(true);
}
}
}
private void getNoOfGrades() {
inputStr = inputNoOfGrades.getText().toString();//.replaceAll(" ", "");
if (inputStr.isEmpty()) {
tvNoOfGrades.setText("no items added");
} else {
// converts string to integer and checks if item is already
// added
numOfGrades = Integer.parseInt(inputStr);
if (numOfGrades > 0) {
inputNoOfGrades.setEnabled(false);
btnNoOfGrades.setEnabled(false);
tvGrades.setText("Enter " + numOfGrades
+ " student grades between 1-10");
}
}
}
private void bubbleSort(ArrayList<Integer> grades) {
for (int i = grades.size() - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (grades.get(j) > grades.get(j + 1)) {
int temp = grades.get(j);
grades.set(j, grades.get(j + 1));
grades.set(j + 1, temp);
}
}
}
}
}
答案 0 :(得分:1)
Collection.toArray()
返回Object[]
。您需要使用Collection.toArray(T[])
来获取特定对象类型的数组:
list.setListData(grades.toArray(new Integer[grades.size()]));