覆盖预定义的数组 [英] Overwriting pre-defined array

查看:86
本文介绍了覆盖预定义的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为columnsArray []的数组,它预先定义为包含6个字符串.当我运行我的方法column()时,它应该用用户通过选中复选框选择的新字符串数组覆盖columnArray []. 我尝试实现此方法的方式将选中的每个框添加到arrayList,然后将arrayList转换为array [].但是,在运行代码时,不会覆盖columnArray.

I have an array named columnsArray[] it is pre-defined to contain 6 Strings. When i run my method columns() it should overwrite the columnsArray[] with a new array of Strings that the user selects by checking boxes. The way i have tried to implement this adds each box checked to an arrayList and then convert the arrayList to array[]. However when the code is run, columnsArray is not overwritten.

到目前为止,这是我的代码:

Here is my code so far:

public class EditView {
    private JFrame frame;
    JCheckBox appNo, name, program, date, pName, country, fileLoc, email, uni,
            countryUni, degree, classification, funding, supervisor,
            rejectedBy, misc;
    public ArrayList<String> columnArrLst;
    public String[] columnsArray = { "Application Number", "Name", "Program",
            "Date", "Project Name", "Country of Uni" };

    public EditView() {

    }

    public void makeFrame() {
        frame = new JFrame("Edit View");
        frame.setPreferredSize(new Dimension(300, 350));
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 2, 20, 20));

        appNo = new JCheckBox("appNo");
        name = new JCheckBox("Name");
        program = new JCheckBox("Program");
        date = new JCheckBox("Date");
        pName = new JCheckBox("Project Title");
        country = new JCheckBox("Country of Origin");
        fileLoc = new JCheckBox("Current File Location");
        // countryRef = new JCheckBox("");
        email = new JCheckBox("Email address");
        uni = new JCheckBox("Last University");
        countryUni = new JCheckBox("Country of last Uni");
        degree = new JCheckBox("Degree");
        classification = new JCheckBox("Degree Classification");
        funding = new JCheckBox("funding");
        supervisor = new JCheckBox("Supervisor");
        rejectedBy = new JCheckBox("Rejected By");
        misc = new JCheckBox("Miscelaneous");
        contentPane.add(appNo);
        contentPane.add(name);
        contentPane.add(program);
        contentPane.add(date);
        contentPane.add(pName);
        contentPane.add(country);
        contentPane.add(fileLoc);
        contentPane.add(email);
        contentPane.add(uni);
        contentPane.add(countryUni);
        contentPane.add(degree);
        contentPane.add(classification);
        contentPane.add(supervisor);
        contentPane.add(rejectedBy);
        contentPane.add(misc);

        JButton changeView = new JButton("Change View");

        changeView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                columns();
                frame.dispose();
            }
        });

        contentPane.add(changeView);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public String[] columns() {
        columnArrLst = new ArrayList<String>();
        if (appNo.isSelected()) {
            columnArrLst.add("AppNo");
        }
        if (name.isSelected()) {
            columnArrLst.add("Name");
        }
        if (date.isSelected()) {
            columnArrLst.add("Date");
        }
        if (fileLoc.isSelected()) {
            columnArrLst.add("file Location");
        }
        if (country.isSelected()) {
            columnArrLst.add("Country");
        }
        if (email.isSelected()) {
            columnArrLst.add("Email");
        }
        if (uni.isSelected()) {
            columnArrLst.add("University");
        }
        if (countryUni.isSelected()) {
            columnArrLst.add("Country of Uni");
        }
        if (degree.isSelected()) {
            columnArrLst.add("Degree");
        }
        if (classification.isSelected()) {
            columnArrLst.add("Degree Classification");
        }
        if (pName.isSelected()) {
            columnArrLst.add("ProjectName");
        }
        if (funding.isSelected()) {
            columnArrLst.add("Funding");
        }
        if (supervisor.isSelected()) {
            columnArrLst.add("Supervisor");
        }
        if (rejectedBy.isSelected()) {
            columnArrLst.add("rejected By");
        }
        if (misc.isSelected()) {
            columnArrLst.add("Miscelaneous");
        }
        columnsArray = new String[columnArrLst.size()];
        columnArrLst.toArray(columnsArray);
        return columnsArray;
    }
}

有什么想法为什么不覆盖?感谢您的帮助.

Any ideas why it isn't overwriting? Thanks for any help.

推荐答案

尝试这个

columnsArray =  columnArrLst.toArray(new String[columnArrLst.size()]);

希望有帮助.

这篇关于覆盖预定义的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆