我对 Java 比较陌生,所以如果我犯了任何明显的错误,我深表歉意。我试图将 ArrayList“数字”从“IO”类传递到“交互”类。我正在尝试将数字的内容打印到控制台作为测试。当我调用 getNumbers 方法时,控制台保持为空。我究竟做错了什么?这是我所拥有的:
IO 类:
import java.io.*;
import java.util.*;
public class IO {
public static void main (String[] args){
fileInput();
}
public static void fileInput() {
//variable setup
String file = "src/seats.txt";
int number;
ArrayList<Integer> numbers = new ArrayList<Integer>();
//Read file into the ArrayLists
try {
Scanner input = new Scanner(new FileReader(file));
while (input.hasNextLine()){ //read a line from file
number = input.nextInt();
input.nextLine();
numbers.add(number);
}
input.close();
}
catch (IOException e){
System.out.println(e);
}
public static ArrayList<Integer> getNumbers(){
ArrayList<Integer> numbers = new ArrayList<Integer>();
return numbers;
}
}
交互类:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Interaction {
public static void main(String[] args) {
menu();
IO.fileInput();
}
static Scanner consoleInput = new Scanner (System.in);
public static void menu (){
String userChoice = "";
while (!userChoice.equals("QUIT")){
System.out.println("| MAIN MENU |");
System.out.println("|-----------------------------------|");
System.out.println("| |");
System.out.println("| 1.) Reserve a Seat |");
System.out.println("| 2.) Cancel a Reservation |");
System.out.println("| 3.) View Waiting List |");
System.out.println("| Quit.) Exit Program |");
System.out.println("| v0.1 |");
System.out.println("\nPlease make your selection:");
userChoice = consoleInput.next().toUpperCase();
switch(userChoice){
default:
if (!userChoice.equals("1") && !userChoice.equals("2") && !userChoice.equals("3")){
System.out.println("Invalid option, please try again.");
}
pause();
break;
case "1":
if (userChoice.equals("1")){
reserveSeat();
}
pause();
break;
case "2":
if(userChoice.equals("2")){
getNumbers();
cancel();
}
pause();
break;
case "3":
if(userChoice.equals("3")){
waitingList();
}
pause();
break;
case "QUIT":
System.out.println("Thank you for using our service!");
consoleInput.close();
}
}
}
private static void reserveSeat() {
String type, direction, userChoice = null;
int number;
private static void cancel() {
System.out.println("Test 2");
}
private static void waitingList() {
System.out.println("Test 3");
}
private static void pause(){
System.out.println("Press enter to return to the menu.");
Scanner enter = new Scanner(System.in);
enter.nextLine();
}
public static void getNumbers(){
ArrayList<Integer> numbers = IO.getNumbers();
System.out.println();
int sz = numbers.size();
for (int i = 0; i < sz; i++){
System.out.println(numbers.get(i).toString());
}
}
}
我知道以前也有人问过类似的问题,但我找不到任何特别有用的东西。任何帮助将不胜感激。
编辑:现在显示整个 Interaction 类,而不仅仅是 getNumbers()
最佳答案
您希望相同的数字
在IO.fileInput()
和IO.getNumbers()
中可见>;您可以添加一个类(static
)numbers
字段,并在两个地方使用it(并编程到List
) > 接口(interface))。类似的东西
private static List<Integer> numbers = new ArrayList<>();
public static List<Integer> getNumbers(){
return numbers;
}
然后,不要忘记从 fileInput
中删除 numbers
声明(以便它使用 static
字段)。
关于java - 将 ArrayList 从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36388113/