如何从 .txt 文件中读取数据并将其放入对象的数组列表中?

3

目前为止,我所写的代码只是基于我对基本数组的了解而实现的,但是我并不知道如何使用 ArrayList 或者从文件中读取数据。虽然当前的代码可以工作,但是如果有关于如何从文件中读取数据和使用 ArrayList 的链接或建议,那将不胜感激。谢谢!


public class TestPackages
{
   public static void main (String[] args) 
    {
       Packages testPackages = new Packages();

       testPackages.addPacket(1001, 7.37, "CA");
       testPackages.addPacket(1002, 5.17, "CT");
       testPackages.addPacket(1003, 11.35, "NY");
       testPackages.addPacket(1004, 20.17, "MA" );
       testPackages.addPacket(1005, 9.99, "FL");
       testPackages.addPacket(1006, 14.91, "VT");
       testPackages.addPacket(1007, 4.97, "TX");

       System.out.println(testPackages); 
    }
}

-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class Packages
{
   private Packet[] shipment;
   private int count;
   private double totalWeight;

   public Packages()
   {
      shipment = new Packet[100];
      count = 0;
      totalWeight = 0.0;
   }

   public void addPacket (int idNumber, double weight, String state)
   {
      if(count == shipment.length)
         increaseSize();

      shipment[count] = new Packet (idNumber, weight, state);
      totalWeight += weight;
      count++;
   }

   public String toString()
   {
      String report;

      report = "All Packets\n";
      for(int num = 0; num < count; num++)
         report += shipment[num].toString() + "\n";

      report += "Total Weight:\t" +totalWeight+" pounds";
      return report;
   }

   private void increaseSize()
   {
      Packet[] temp = new Packet[shipment.length * 2];

      for (int num = 0; num < shipment.length; num++)
         temp[num] = shipment[num];

      shipment = temp;
   } 
}

-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

public class Packet
{
   private int idNumber;
   private double weight;
   private String state;

   public Packet(int idNumber, double weight, String state)
   {
      this.idNumber = idNumber;
      this.weight = weight;
      this.state = state;
   }

   public boolean isHeavy(double weight)
   {
      return (weight > 10);
   }

   public boolean isLight(double weight)
   {
      return (weight < 7);
   }

   public String toString()
   {
      String description = "ID number: " + idNumber + "\tWegiht: " + weight + "\tState: " 
      + state;

      return description;
   }
}
2个回答

1

尝试使用此链接学习如何使用JAVA从文件中读取内容

如何使用Java逐行读取大型文本文件?

您可以尝试以下代码来学习如何使用Arraylist(网络上还有更多的教程)

Packet p1=new Packet (1001, 7.37, "CA");
Packet p2=new Packet (1002, 17.5, "SF");
Packet p3=new Packet (1003, 13.8, "DF");
Packet p4=new Packet (1004, 14.4, "XC");

ArrayList<Packet> arr= new ArrayList<Packet>();

arr.add(p1);
arr.add(p2);
arr.add(p3);
arr.add(1,p4);

System.out.println("return the 2st element- " + arr.get(1) );
System.out.println("return the 4rd element- " + arr.get(3) );

System.out.println("Size of ArrayList after insertion- " + arr.size());
System.out.println("Elements of ArrayList after insertion- " + arr);

arr.remove(p2); 
arr.remove(2);

System.out.println("Size of ArrayList after deletion- " + arr.size());
System.out.println("Elements of ArrayList after deletion- " + arr);

1

有很多种方法可以在Java中逐行读取文件。但我更喜欢使用commons-io工具包。这是一个非常有用的工具,可以从文件中读取内容。下面是一个例子。

List<String> lines = FileUtils.readLines(new File(fileName));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接