【问题标题】:How to add remaining batch of n elements into arrayList?如何将剩余的 n 个元素添加到 arrayList 中?
【发布时间】:2022-06-10 19:28:32
【问题描述】:

我目前正在学习开发一个简单的区块链程序,该程序从 .txt 中读取样本数据,并为每 10 笔交易创建一个新区块。我想知道给定的示例数据是否是 23 行交易,有没有办法制作一个包含最后 3 笔交易的新区块?

电流输出

Block[header=Header[index=0,currHash=51aa6b7cf5fb821189d58b5c995b4308370888efcaac469d79ad0a5d94fb0432, prevHash=0, timestamp=1654785847112], tranx=null]

Block[header=Header[index=0,currHash=92b3582095e2403c68401448e8a34864e8465d0ea51c05f11c23810ec36b4868, prevHash=0, timestamp=1654785847385], tranx=Transaction [tranxLst=[alice|bob|credit|1.0, alice|bob|debit|2.0, alice|bob|debit|3.0, alice|bob|credit|4.0, alice|bob|debit|5.0, alice|bob|credit|6.0, alice|bob|debit|7.0, alice|bob|debit|8.0, alice|bob|debit|9.0, alice|bob|debit|10.0]]]

Block[header=Header[index=0,currHash=7488c600433d78e0fb8586e71a010b1d39a040cb101cc6e3418668d21b614519, prevHash=0, timestamp=1654785847386], tranx=Transaction [tranxLst=[alice|bob|credit|11.0, alice|bob|credit|12.0, alice|bob|debit|13.0, alice|bob|debit|14.0, alice|bob|credit|15.0, alice|bob|credit|16.0, alice|bob|credit|17.0, alice|bob|debit|18.0, alice|bob|credit|19.0, alice|bob|credit|20.0]]]

我想要什么

Block[header=Header[index=0,currHash=51aa6b7cf5fb821189d58b5c995b4308370888efcaac469d79ad0a5d94fb0432, prevHash=0, timestamp=1654785847112], tranx=null]

Block[header=Header[index=0,currHash=92b3582095e2403c68401448e8a34864e8465d0ea51c05f11c23810ec36b4868, prevHash=0, timestamp=1654785847385], tranx=Transaction [tranxLst=[alice|bob|credit|1.0, alice|bob|debit|2.0, alice|bob|debit|3.0, alice|bob|credit|4.0, alice|bob|debit|5.0, alice|bob|credit|6.0, alice|bob|debit|7.0, alice|bob|debit|8.0, alice|bob|debit|9.0, alice|bob|debit|10.0]]]   

Block[header=Header[index=0,currHash=7488c600433d78e0fb8586e71a010b1d39a040cb101cc6e3418668d21b614519, prevHash=0, timestamp=1654785847386], tranx=Transaction [tranxLst=[alice|bob|credit|11.0, alice|bob|credit|12.0, alice|bob|debit|13.0, alice|bob|debit|14.0, alice|bob|credit|15.0, alice|bob|credit|16.0, alice|bob|credit|17.0, alice|bob|debit|18.0, alice|bob|credit|19.0, alice|bob|credit|20.0]]]

Block[header=Header[index=0,currHash=7488c600433d78e0fb8586e71a010b1d39a040cb101cc6e3418668d21b614520, prevHash=0, timestamp=1654785847387], tranx=Transaction [tranxLst=[alice|bob|credit|21.0, alice|bob|credit|22.0, alice|bob|debit|23.0]]]

我的代码: 客户端应用

public static void main(String[] args) throws IOException {
        homework();
    }

static void homework() throws IOException {
    int count = 0;
    Transaction tranxLst = new Transaction();
    Block genesis = new Block("0");
    System.out.println(genesis);

    BufferedReader bf = new BufferedReader(new FileReader("dummytranx.txt"));
    String line = bf.readLine();
    while (line != null) {
        tranxLst.add(line);
        line = bf.readLine();
        count++;
        if (count % 10 == 0) {
            Block newBlock = new Block(genesis.getHeader().getPrevHash());
            newBlock.setTranx(tranxLst);
            System.out.println(newBlock);
            tranxLst.getTranxLst().clear();
        }
    }
    bf.close();

}

事务类

public class Transaction implements Serializable {

    public static final int SIZE = 10;
    /**
     * we will comeback to generate the merkle root ie., hash of merkle tree
     * merkleRoot = hash
     */
    private String merkleRoot = "9a0885f8cd8d94a57cd76150a9c4fa8a4fed2d04c244f259041d8166cdfeca1b8c237b2c4bca57e87acb52c8fa0777da";
//  private String merkleRoot;  

    public String getMerkleRoot() {
        return merkleRoot;
    }

    public void setMerkleRoot(String merkleRoot) {
        this.merkleRoot = merkleRoot;
    }

    /**
     * For the data collection, u may want to choose classic array or collection api
     */
    private List<String> tranxLst;

    public List<String> getTranxLst() {
        return tranxLst;
    }

    public Transaction() {
        tranxLst = new ArrayList<>(SIZE);
    }

    /**
     * add()
     */
    public void add(String tranx) {
        tranxLst.add(tranx);
    }

    @Override
    public String toString() {
        return "Transaction [tranxLst=" + tranxLst + "]";
    }

}

方块类

public class Block implements Serializable {

    private Header header;

    public Header getHeader() {
        return header;
    }

    private Transaction tranx;

    public Block(String previousHash) {
        header = new Header();
        header.setTimestamp(new Timestamp(System.currentTimeMillis()).getTime());
        header.setPrevHash(previousHash);
        String blockHash = Hasher.sha256(getBytes());
        header.setCurrHash(blockHash);
    }

    /**
     * getBytes of the Block object
     */
    private byte[] getBytes() {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(baos);) {
            out.writeObject(this);
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Transaction getTranx() {
        return tranx;
    }

    /**
     * aggregation rel
     */
    public void setTranx(Transaction tranx) {
        this.tranx = tranx;
    }

    /**
     * composition rel
     */
    public class Header implements Serializable {
        private int index;
        private String currHash, prevHash;
        private long timestamp;

        // getset methods
        public String getCurrHash() {
            return currHash;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public void setCurrHash(String currHash) {
            this.currHash = currHash;
        }

        public String getPrevHash() {
            return prevHash;
        }

        public void setPrevHash(String prevHash) {
            this.prevHash = prevHash;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }

        @Override
        public String toString() {
            return "Header [index=" + index + ", currHash=" + currHash + ", prevHash=" + prevHash + ", timestamp="
                    + timestamp + "]";
        }

    }

    @Override
    public String toString() {
        return "Block [header=" + header + ", tranx=" + tranx + "]";
    }

}
enter code here

【问题讨论】:

    标签: java


    【解决方案1】:

    不要在条件语句中使用计数器,而是尝试 ForLoop。

    static void homework() throws IOException {
        Transaction tranxLst = new Transaction();
        Block genesis = new Block("0");
        System.out.println(genesis);
        BufferedReader bf = new BufferedReader(new FileReader("dummytranx.txt"));
        String line = bf.readLine();
        while (line != null) {
            for (int i = 0; i < 10; i++) {
                tranxLst.add(line);
                line = bf.readLine();
                if (line == null) {
                    break;
                }
            }
            Block newBlock = new Block(genesis.getHeader().getPrevHash());
            newBlock.setTranx(tranxLst);
            System.out.println(newBlock);
            tranxLst.getTranxLst().clear();
        }
        bf.close();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多