如何从另一个类的方法获取变量值

时间:2019-04-26 03:32:51

标签: java

我有一个名为ReadFile的类,该类具有ReadFile函数,该函数随后调用了另外两个返回元素和数字的方法。如何从ReadFile类中获取这些值,以便传递给主类中的方法?

public static void main(String[] args)  {

        List<Integer> numbers = new ArrayList<>();
        List<Integer> elements = new ArrayList<>();

        ReadFile numbersFile = new ReadFile();
        numbersFile.readFile();

        preSortedList(numbers, elements);

        System.out.println();
        sortedList(numbers, elements);

} 


public class ReadFile {


   public void readFile(){

        File folder = new File("/Users/Mary/NetBeansProjects/Testing/src/program/pkg4/Input");

        for (File file : folder.listFiles()) {
            try{
                FileReader fileReader = new FileReader(file);
                BufferedReader reader = new BufferedReader(fileReader);

                List<Integer> elements = getElements(reader);
                List<Integer> numbers = getNumbers(reader, elements);

                reader.close(); 
            }catch(IOException e){
                System.out.println("ERROR: There was a problem reading the file.\n" + e.getMessage());
            }  
        }
    }      

    public static List<Integer> getElements(BufferedReader reader) throws IOException{
        List<Integer> elements = new ArrayList<>();
        String numberOfElement = reader.readLine();
        elements.add(Integer.parseInt(numberOfElement));
        return elements;
    }

    public static List<Integer> getNumbers(BufferedReader reader, List elements) throws IOException{
        List<Integer> numbers = new ArrayList<>();
        String line = reader.readLine();                  
        for (String s : line.split("\\s+")) {                        
            numbers.add(Integer.parseInt(s));
        }    
        return numbers;
    } 
}

2 个答案:

答案 0 :(得分:0)

在ReadFile类中创建两个成员变量,名称元素和数字,然后在main方法中创建ReadFile类的对象,并使用getter获取其成员的值。

public class ReadFile {
    List<Integer> elements
    List<Integer> numbers

      //Constructors

     //create getter and setters here


    public void readFile(){

    File folder = new File("/Users/Mary/NetBeansProjects/Testing/src/program/pkg4/Input");

    for (File file : folder.listFiles()) {
        try{
            FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader);

            elements = getElements(reader);
            numbers = getNumbers(reader, elements);

            reader.close(); 
        }catch(IOException e){
            System.out.println("ERROR: There was a problem reading the file.\n" + e.getMessage());
        }  
    }
}      

public static List<Integer> getElements(BufferedReader reader) throws IOException{
    List<Integer> elements = new ArrayList<>();
    String numberOfElement = reader.readLine();
    elements.add(Integer.parseInt(numberOfElement));
    return elements;
}

public static List<Integer> getNumbers(BufferedReader reader, List elements) throws IOException{
    List<Integer> numbers = new ArrayList<>();
    String line = reader.readLine();                  
    for (String s : line.split("\\s+")) {                        
        numbers.add(Integer.parseInt(s));
    }    
    return numbers;
} 

}

然后使用您的主要方法

public static void main(String[] args)  {

    ReadFile numbersFile = new ReadFile();
    numbersFile.readFile();

    preSortedList(numbersFile.getNumbers(), numbersFile.getElements);

    System.out.println();
    sortedList(numbersFile.getNumbers(), numbersFile.getElements);

}

答案 1 :(得分:0)

如我所见,有两种不同的方法。

方法1 :使用参考

更改readFile()方法以接受数字和元素列表作为输入参数。 像这样:

public void readFile(List<Integer> numbers, List<Integer> elements){}

在您的主类中,传递以下两个参数:

public static void main(String[] args)  {
    List<Integer> numbers = new ArrayList<>();
    List<Integer> elements = new ArrayList<>();

    ReadFile numbersFile = new ReadFile();
    numbersFile.readFile(numbers,elements);
    preSortedList(numbers, elements);

    System.out.println();
    sortedList(numbers, elements);
}

在您的readFile()方法中:

public void readFile(List<Integer> numbers,List<Integer> elements) {
     File folder = new File("/Users/Mary/NetBeansProjects/Testing/src/program/pkg4/Input");

     for (File file : folder.listFiles()) {
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader reader = new BufferedReader(fileReader);

                elements = getElements(reader);
                numbers  = getNumbers(reader, elements);

                reader.close(); 
            }catch(IOException e){
                System.out.println("ERROR: There was a problem reading the file.\n" + e.getMessage());
            }  
        }
    } 

从Main类传递来的数字和元素列表应使用引用的概念来更新值。

方法2 :使用类成员变量

在您的List<Integer>类中添加2个类型为ReadFile的类成员,并为其创建getter和setter:

public class ReadFile {
    List<Integer> elements;
    List<Integer> numbers;

     //getter and setters here

    public void readFile(){

    File folder = new File("/Users/Mary/NetBeansProjects/Testing/src/program/pkg4/Input");

    for (File file : folder.listFiles()) {
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader);

            elements = getElements(reader);
            numbers = getNumbers(reader, elements);

            reader.close(); 
        }catch(IOException e){
            System.out.println("ERROR: There was a problem reading the file.\n" + e.getMessage());
        }  
    }
} 

现在,在主要方法中,您可以使用吸气剂来使用它们。

还有另一种幼稚的方法可以在这里使用。

方法3 :使用集合/数据结构存储列表2的值并返回它们。

public ArrayList<Integer>[] readFile(List<Integer> numbers,List<Integer> elements){

            File folder = new File("/Users/Mary/NetBeansProjects/Testing/src/program/pkg4/Input");

            for (File file : folder.listFiles()) {
                try{
                    FileReader fileReader = new FileReader(file);
                    BufferedReader reader = new BufferedReader(fileReader);

                    elements = getElements(reader);
                    numbers  = getNumbers(reader, elements);

                    reader.close(); 
                }catch(IOException e){
                    System.out.println("ERROR: There was a problem reading the file.\n" + e.getMessage());
                }

    ArrayList<Integer>[] al = new ArrayList[2];             
    al[0] = elements;
    al[1] = numbers;

    return al;
            }
        } 

在主类中:

public static void main(String[] args)  {

        List<Integer> numbers = new ArrayList<>();
        List<Integer> elements = new ArrayList<>();

        ReadFile numbersFile = new ReadFile();
        List<Integer>[] al = numbersFile.readFile();

        elements = al[0];
        numbers = al[1];

        preSortedList(numbers, elements);

        System.out.println();
        sortedList(numbers, elements);

} 

编辑:此外,似乎只有元素和数字才应仅具有来自上次文件迭代的值,因为每次迭代都将覆盖现有值,而不是附加新值。

需要更改私有方法以处理相同的方法。

public static List<Integer> getElements(BufferedReader reader) throws IOException{

// check if elements have been initialised or not. If not then create new memory for that else append to the existing list.

if(null == elements){
         elements = new ArrayList<>();}
        String numberOfElement = reader.readLine();
        elements.add(Integer.parseInt(numberOfElement));
        return elements;
    }

    public static List<Integer> getNumbers(BufferedReader reader, List elements) throws IOException{

// check if numbers have been initialised or not 
if(null == numbers ){
         numbers = new ArrayList<>();}

        String line = reader.readLine();                  
        for (String s : line.split("\\s+")) {                        
            numbers.add(Integer.parseInt(s));
        }    
        return numbers;
    }