日期:2014-05-20  浏览次数:20925 次

新手求助数据类型存储的问题
小弟现在刚刚开始学习java

想读取文件并且存储在一个地方,要求是动态的

文件每一行每一行的读 里面包含了很多参数需要提取 如下 

  private String Name;
  private String InputA;
  private String InputB;
  private String InputC;
  private String InputD;
  private String OutputZ;
  private String OutputNZ;
  private boolean Used; 

因为每一行就包含如上内容 请问如何动态的存储 因为不确定有多少行。。



------解决方案--------------------
Java code

package a;

import java.util.ArrayList;
import java.util.List;

class VO {
    private String name;
    private String inputA;
    private String inputB;
    private String inputC;
    private String inputD;
    private String outputZ;
    private String outputNZ;
    private boolean used;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInputA() {
        return inputA;
    }

    public void setInputA(String inputA) {
        this.inputA = inputA;
    }

    public String getInputB() {
        return inputB;
    }

    public void setInputB(String inputB) {
        this.inputB = inputB;
    }

    public String getInputC() {
        return inputC;
    }

    public void setInputC(String inputC) {
        this.inputC = inputC;
    }

    public String getInputD() {
        return inputD;
    }

    public void setInputD(String inputD) {
        this.inputD = inputD;
    }

    public String getOutputZ() {
        return outputZ;
    }

    public void setOutputZ(String outputZ) {
        this.outputZ = outputZ;
    }

    public String getOutputNZ() {
        return outputNZ;
    }

    public void setOutputNZ(String outputNZ) {
        this.outputNZ = outputNZ;
    }

    public boolean isUsed() {
        return used;
    }

    public void setUsed(boolean used) {
        this.used = used;
    }

    public VO() {

    }

    public VO(String name, String inputA, String inputB, String inputC,
            String inputD, String outputZ, String outputNZ, boolean used) {
        this.name = name;
        this.inputA = inputA;
        this.inputB = inputB;
        this.inputC = inputC;
        this.inputD = inputD;
        this.outputZ = outputZ;
        this.outputNZ = outputNZ;
        this.used = used;
    }

    @Override
    public String toString() {
        return "VO [name=" + name + ", inputA=" + inputA + ", inputB=" + inputB
                + ", inputC=" + inputC + ", inputD=" + inputD + ", outputZ="
                + outputZ + ", outputNZ=" + outputNZ + ", used=" + used + "]";
    }
    
    

}

public class VOS {
    private List list = new ArrayList();

    public List getList() {
        return list;
    }

    /**
     * 如果name 是唯一的,则通过名字索引查找
     */
    public VO findByName(String name) {

        VO vo = null;
        for (int i = 0; i < list.size(); i++) {
            vo = (VO) list.get(i);
            if (vo.getName().equals(name)) {
                return vo;
            }
        }

        return null;
    }

    /**
     * 读取文件放到list中
     * 
     * @author sunstar
     * @date 2112-7-4 上午11:19:54
     */
    public void loadVos() {

        list.clear();
        /*while (true 文件还没读完 ) {
            VO vo = new VO();
            vo.setName("A");
            // ...给该vo设置参数
            list.add(vo);
        }*/
        //一下为测试数据
        VO vo = new VO("A0", "inputA0", "inputB0", "inputC0", "inputD0", "outputZ0", "outputNZ0" , false) ;
        list.add(vo) ;
        vo = new VO("A1", "inputA1", "inputB1", "inputC1", "inputD1", "outputZ1", "outputNZ1" , true) ;
        list.add(vo) ;
        vo = new VO("A2", "inputA2", "inputB2", "inputC2", "inputD2", "outputZ2", "outputNZ2" , true) ;
        list.add(vo) ;
        vo = new VO("A3", "inputA3", "inputB3", "inputC3", "inputD3", "outputZ3", "outputNZ3" , false) ;
        list.add(vo) ;
    }

    /**
     * @param args
     * @author sunstar
     * @date 2112-7-4 上午11:17:18
     */
    public static void main(String[] args) {
        
        VOS vos = new VOS() ;
        vos.loadVos() ;//加载数据
        
        VO vo = vos.findByName("A0");
        System.out.println("查找name = A0的数据:" + vo); 
        vo = vos.findByName("A5");
        System.out.println("查找name = A5的数据:" + vo); 
        //如果没找到 应该打印null,查找效率为N/2 ,最好为1 ,即第一个 ,最差为 没找到 ,需要遍历N遍
        
        
        

    }

}