gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:17:17 26 4
gpt4 key购买 nike

我有一个名为 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;
}
}

最佳答案

在你的ReadFile类中创建两个成员变量name elements和numbers,然后在你的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);

}

关于java - 如何从另一个类的方法中获取变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55860457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com