从覆盖传递数组

时间:2014-02-26 05:30:22

标签: java

我知道我应该理解如何做到这一点但是对于我的生活我似乎无法传递int [] array1并将其设置为等于int [] toSort down down。当试图通过时,我不断找到错误变量。感谢您的支持和帮助。

public class LoadListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // Let the user pick the file
            JFileChooser pickFile = new JFileChooser();
            String fileName = "boo";
            Scanner input;
            if(pickFile.showOpenDialog(mPanel) == JFileChooser.APPROVE_OPTION) {
                fileName = pickFile.getSelectedFile().getAbsolutePath();
                System.out.println(fileName);
                try {
                    input = new Scanner(new File(fileName));
                    StringBuffer data = new StringBuffer("");
                    while(input.hasNextLine()) {
                        String t = input.nextLine();
                        System.out.println("Line to add: " + t);
                        data.append(t);

                    }

                    input.close();  
                    unsortedList.setText(data.toString());

                    String[] ss = ((unsortedList.getText()).replaceAll(" ",     ",")).split(fileName);                   

                    int[] array1 = new int[ss.length];
                    for (int i=0; i < ss.length; i++) {
                         array1[i] = Integer.parseInt(ss[i]);
                    }




                }
                catch(FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(mPanel, "Error opening" +
                            " and/or processing file: " + fileName,
                            "File Error", JOptionPane.ERROR_MESSAGE);
                    System.out.println(ex);
                }
            }
        }
    }


    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            StringBuffer dataSorted = new StringBuffer("");

            // TODO: Get the list from the TextArea
            int[] toSort = array1;

2 个答案:

答案 0 :(得分:1)

array1仅在actionPerformed()类的LoadListener方法中具有范围,并且您正在从另一个类ButtonListener的另一个方法访问它。这在java中是非法的

您无法从另一个方法访问一个方法局部变量。有关更多详细信息,请阅读oracle doc

答案 1 :(得分:0)

@ user3354341:尝试以下代码,它将解决您的问题,并且您还需要了解java中变量的范围,因为您尝试将一个类的局部变量访问到另一个类中。

public class LoadListener implements ActionListener {

        private int[] array1;

        @Override
        public void actionPerformed(ActionEvent e) {
            ...
            String[] ss = ((unsortedList.getText()).replaceAll(" ",     ",")).split(fileName);
            array1 = new int[ss.length];
            ...
        }
        public int[] getArrayData(){
        return array1;
        }
}

private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            StringBuffer dataSorted = new StringBuffer("");

            LoadListener loadListener = new LoadListener();

            // TODO: Get the list from the TextArea
            int[] toSort = loadListener.getArrayData();
        }
}