如何使用文件读取方法中填充的ArrayList?

问题描述 投票:0回答:1

我做了一个文件读取,并从行的元素中创建了数组、数组列表。问题是,我不能在文件读取方法之外使用这些数组和数组列表。如果我在这个方法中简单地system.out.println()它们,它就能工作,所以这不是 "不能从文件中获取 "的问题,而是不知为何我不能使用它们。我甚至尝试用这些元素来制作对象,但也没有成功。但是system.out.println()也能用,所以我完全搞不懂。

谢谢你的帮助。)

import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
import java.util.InputMismatchException;

public class Vehicle {
        protected String typeOfVehicle;
        protected int lineNum;
        protected String lineLetter;
        protected String way;
        protected boolean articulated;
        protected boolean lowFloor;
        protected double operationCost;
        protected int numOfSeats;
        protected boolean bicycleTransportOpp;
        protected int numOfDisabledPlaces;
        protected boolean needToRepair;
        protected String typeOfFuel;
        protected boolean hasWheel;

        protected static ArrayList<Vehicle> vehicles=new ArrayList<Vehicle>();
        protected static ArrayList<String> vehicleTypes=new ArrayList<>();
        protected static ArrayList<Trolley> trolleys=new ArrayList<>();
        protected static ArrayList<Tram> trams=new ArrayList<>();
        protected static ArrayList<Bus> buses=new ArrayList<>();
        protected static ArrayList<WheelChairAccessible> wheelChairAccessibleVehicles = new ArrayList<>();
        protected static ArrayList<Vehicle> toService = new ArrayList<>();
        protected static ArrayList<Vehicle> bicycleVehicles=new ArrayList<>();
        protected static ArrayList<Vehicle> fossilVehicles=new ArrayList<>();
        protected static ArrayList<Vehicle> electricVehicles=new ArrayList<>();


        public Vehicle( int lineNum, String lineLetter, String way, boolean articulated, boolean lowFloor, double operationCost, int numOfSeats,
                        boolean bicycleTransportOpp, int numOfDisabledPlaces, boolean needToRepair,String typeOfFuel, boolean hasWheel) {
        }

        public static void readIn(String fileName){ 

            try {

                FileReader reader=new FileReader(fileName);
                BufferedReader buffer=new BufferedReader(reader);
                String queue=null;
                int i=0;

                while((queue=buffer.readLine())!=null) {

                        String parts[] = queue.split(",");
                        String typeOfVehicle[]=new String[queue.length()];
                        int[]lineNum=new int[queue.length()];
                        String[]lineLetter=new String[queue.length()];
                        String[]way=new String[queue.length()];
                        boolean[]articulated=new boolean[queue.length()];
                        boolean[]lowFloor=new boolean[queue.length()];
                        double[]operationCost=new double[queue.length()];
                        int[]numOfSeats=new int[queue.length()];
                        boolean[]bicycleTransportOpp=new boolean[queue.length()];
                        int[]numOfDisabledPlaces=new int[queue.length()];
                        boolean[]needToRepair=new boolean[queue.length()];
                        String[]typeOfFuel=new String[queue.length()];
                        boolean[]hasWheel=new boolean[queue.length()];

                        typeOfVehicle[i]=parts[0];
                        vehicleTypes.add(typeOfVehicle[i]);     
                        //System.out.println(vehicleTypes.get(i));

                        lineNum[i]=Integer.parseInt(parts[1]);
                        lineLetter[i]=parts[2];
                        way[i]=parts[3];
                        articulated[i]=Boolean.valueOf(parts[4]);
                        lowFloor[i]=Boolean.parseBoolean(parts[5]);
                        operationCost[i]=Double.parseDouble(parts[6]);
                        numOfSeats[i]=Integer.parseInt(parts[7]);
                        bicycleTransportOpp[i]=Boolean.parseBoolean(parts[8]);
                        numOfDisabledPlaces[i]=Integer.parseInt(parts[9]);
                        needToRepair[i]=Boolean.parseBoolean(parts[10]);
                        typeOfFuel[i]=parts[11];
                        hasWheel[i]=Boolean.parseBoolean(parts[12]);

                        Vehicle vehicle=new Vehicle( lineNum[i], lineLetter[i], way[i], articulated[i], lowFloor[i],  operationCost[i],  numOfSeats[i],
                                bicycleTransportOpp[i],  numOfDisabledPlaces[i],  needToRepair[i], typeOfFuel[i], hasWheel[i]);
                        vehicles.add(vehicle);  

                        System.out.print(typeOfVehicle[i]+" "+lineNum[i] + " "+lineLetter[i]+ " "+ way[i]+  " "+articulated[i]+ " "+lowFloor[i]+  " "+operationCost[i]+ 
                         " "+numOfSeats[i]+ " "+bicycleTransportOpp[i]+  " "+numOfDisabledPlaces[i]+ " "+needToRepair[i]+ " "+typeOfFuel[i]+" "+hasWheel[i]);
                            System.out.println();                       
                    i++;
                }
                buffer.close();

                for(Vehicle v: vehicles) {

                    if(v.typeOfFuel.equals("electrical energy") && v.hasWheel) {
                            trolleys.add((Trolley) v);
                            System.out.println("trolleys: "+v.toString());
                    }
                    else if(v.typeOfFuel.equals("electrical energy") && !v.hasWheel) {
                            trams.add((Tram) v);
                            System.out.println("trams: "+v.toString());
                    }
                    else if(v.typeOfFuel.equals("petrol") || v.typeOfFuel.equals("diesel oil") && v.hasWheel) {
                            buses.add((Bus) v);
                            System.out.println("buses: "+v.toString());
                    }   
                }

            }catch(FileNotFoundException e) {
                System.out.println("File not found.");
            }catch(IOException e) {
                System.out.println("e.getMessage()");
            }catch (InputMismatchException exception) {
                System.out.println("Not appropriate input type.");
            }       
        }

        public static void vehicleTypes() {

            for(int i=0;i<vehicleTypes.size();i++) {
                System.out.println(vehicleTypes.get(i));
            }   
        }
        public String toString() {

            return typeOfVehicle+" "+lineNum+" "+lineLetter+" "+way+" "+articulated+" "+ lowFloor+" "+  operationCost
                    +" "+numOfSeats+" "+    bicycleTransportOpp+" "+numOfDisabledPlaces
                    +" "+needToRepair+" "+typeOfFuel+" "+hasWheel;
        }
        public static void main(String[] args) {

            readIn("C:\\Users\\geono\\eclipse-workspace\\Tomegkozlekedes\\classes files\\vehicles.txt");
            vehicleTypes();
        }   
    }

arraylist filereader
1个回答
0
投票

而不是使用

protected static ArrayList<Trolley> trolleys = new ArrayList<>();

使用:

public static ArrayList trolleys = new ArrayList<>();

对于每一个ArrayList。

不同的是 ArrayList 没有 <Trolley> 而不是 protected staticpublic static

我在eclipse中测试你的代码时发现了这个问题。

下面是准备好的代码。

import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;

public class Vehicle {
        protected String typeOfVehicle;
        protected int lineNum;
        protected String lineLetter;
        protected String way;
        protected boolean articulated;
        protected boolean lowFloor;
        protected double operationCost;
        protected int numOfSeats;
        protected boolean bicycleTransportOpp;
        protected int numOfDisabledPlaces;
        protected boolean needToRepair;
        protected String typeOfFuel;
        protected boolean hasWheel;

        public static ArrayList vehicles=new ArrayList<>();
        public static ArrayList vehicleTypes=new ArrayList<>();
        public static ArrayList trolleys=new ArrayList<>();
        public static ArrayList trams=new ArrayList<>();
        public static ArrayList buses=new ArrayList<>();
        public static ArrayList wheelChairAccessibleVehicles = new ArrayList<>();
        public static ArrayList toService = new ArrayList<>();
        public static ArrayList bicycleVehicles=new ArrayList<>();
        public static ArrayList fossilVehicles=new ArrayList<>();
        public static ArrayList electricVehicles=new ArrayList<>();


        public Vehicle( int lineNum, String lineLetter, String way, boolean articulated, boolean lowFloor, double operationCost, int numOfSeats,
                        boolean bicycleTransportOpp, int numOfDisabledPlaces, boolean needToRepair,String typeOfFuel, boolean hasWheel) {
        }

        public static void readIn(String fileName){ 

            try {

                FileReader reader=new FileReader(fileName);
                BufferedReader buffer=new BufferedReader(reader);
                String queue=null;
                int i=0;

                while((queue=buffer.readLine())!=null) {

                        String parts[] = queue.split(",");
                        String typeOfVehicle[]=new String[queue.length()];
                        int[]lineNum=new int[queue.length()];
                        String[]lineLetter=new String[queue.length()];
                        String[]way=new String[queue.length()];
                        boolean[]articulated=new boolean[queue.length()];
                        boolean[]lowFloor=new boolean[queue.length()];
                        double[]operationCost=new double[queue.length()];
                        int[]numOfSeats=new int[queue.length()];
                        boolean[]bicycleTransportOpp=new boolean[queue.length()];
                        int[]numOfDisabledPlaces=new int[queue.length()];
                        boolean[]needToRepair=new boolean[queue.length()];
                        String[]typeOfFuel=new String[queue.length()];
                        boolean[]hasWheel=new boolean[queue.length()];

                        typeOfVehicle[i]=parts[0];
                        vehicleTypes.add(typeOfVehicle[i]);     
                        //System.out.println(vehicleTypes.get(i));

                        lineNum[i]=Integer.parseInt(parts[1]);
                        lineLetter[i]=parts[2];
                        way[i]=parts[3];
                        articulated[i]=Boolean.valueOf(parts[4]);
                        lowFloor[i]=Boolean.parseBoolean(parts[5]);
                        operationCost[i]=Double.parseDouble(parts[6]);
                        numOfSeats[i]=Integer.parseInt(parts[7]);
                        bicycleTransportOpp[i]=Boolean.parseBoolean(parts[8]);
                        numOfDisabledPlaces[i]=Integer.parseInt(parts[9]);
                        needToRepair[i]=Boolean.parseBoolean(parts[10]);
                        typeOfFuel[i]=parts[11];
                        hasWheel[i]=Boolean.parseBoolean(parts[12]);

                        Vehicle vehicle=new Vehicle( lineNum[i], lineLetter[i], way[i], articulated[i], lowFloor[i],  operationCost[i],  numOfSeats[i],
                                bicycleTransportOpp[i],  numOfDisabledPlaces[i],  needToRepair[i], typeOfFuel[i], hasWheel[i]);
                        vehicles.add(vehicle);  

                        System.out.print(typeOfVehicle[i]+" "+lineNum[i] + " "+lineLetter[i]+ " "+ way[i]+  " "+articulated[i]+ " "+lowFloor[i]+  " "+operationCost[i]+ 
                         " "+numOfSeats[i]+ " "+bicycleTransportOpp[i]+  " "+numOfDisabledPlaces[i]+ " "+needToRepair[i]+ " "+typeOfFuel[i]+" "+hasWheel[i]);
                            System.out.println();                       
                    i++;
                }
                buffer.close();

                for(Vehicle v: vehicles) {

                    if(v.typeOfFuel.equals("electrical energy") && v.hasWheel) {

                            trolleys.add((Trolley) v);
                            System.out.println("trolleys: "+v.toString());
                    }
                    else if(v.typeOfFuel.equals("electrical energy") && !v.hasWheel) {
                            trams.add((Tram) v);
                            System.out.println("trams: "+v.toString());
                    }
                    else if(v.typeOfFuel.equals("petrol") || v.typeOfFuel.equals("diesel oil") && v.hasWheel) {
                            buses.add((Bus) v);
                            System.out.println("buses: "+v.toString());
                    }   
                }

            }catch(FileNotFoundException e) {
                System.out.println("File not found.");
            }catch(IOException e) {
                System.out.println("e.getMessage()");
            }catch (InputMismatchException exception) {
                System.out.println("Not appropriate input type.");
            }       
        }

        public static void vehicleTypes() {

            for(int i=0;i<vehicleTypes.size();i++) {
                System.out.println(vehicleTypes.get(i));
            }   
        }
        public String toString() {

            return typeOfVehicle+" "+lineNum+" "+lineLetter+" "+way+" "+articulated+" "+ lowFloor+" "+  operationCost
                    +" "+numOfSeats+" "+    bicycleTransportOpp+" "+numOfDisabledPlaces
                    +" "+needToRepair+" "+typeOfFuel+" "+hasWheel;
        }
        public static void main(String[] args) {

            readIn("C:\\Users\\geono\\eclipse-workspace\\Tomegkozlekedes\\classes files\\vehicles.txt");
            vehicleTypes();
        }   
    }

希望能用得上!

© www.soinside.com 2019 - 2024. All rights reserved.