0

如果代码很长,我很抱歉。 BlockTuple是自定义类。它们的属性受到保护。Block包含一个ArrayListTuples元组具有属性 int Key和 String value

我有 ArrayList[] bucketLstS 和 Block[] bucketBlocksS

ArrayList[] bucketLstS = new ArrayList[Setting.memorySize - 1];
        Block[] bucketBlocksS = new Block[Setting.memorySize - 1];

        for (int i = 0; i < bucketBlocksS.length; i++) {
            bucketLstS[i] = new ArrayList<Tuple>();
            bucketBlocksS[i] = new Block();
        }

        //Get individual relS keys
        RelationLoader sLoader = relS.getRelationLoader();
        while (sLoader.hasNext()) {

            Block[] blocks = sLoader.loadNextBlocks(1);

            for (Block b : blocks) {
                numIO++;
                if (b != null) {

                    for (Tuple t : b.tupleLst) {

                        //Hash the key to determine which Bucket
                        bucketIdx = t.key % (Setting.memorySize - 2);

                        //System.out.println(bucketBlocksS[bucketIdx].getNumTuples());
                        //check if the block is already full
                        if (bucketBlocksS[bucketIdx].getNumTuples() >= Setting.blockFactor) {
                            bucketLstS[bucketIdx].add(bucketBlocksS[bucketIdx]);
                            bucketBlocksS[bucketIdx] = new Block();
                            blkNum++;
                        }
                        bucketBlocksS[bucketIdx].insertTuple(t);
                    }
                }
            }
        }//end while

问题是我需要循环进入每个块,以便我可以访问每个元组的我试过这个:

 for (int i = 0; i < bucketLstS.length; i++) {
             System.out.println(i + " " + bucketLstS[i]);
            for (int j = 0; j < bucketLstS[i].size(); j++) {
                //Access a Block in Block Array
                System.out.println(j + " " + bucketLstS[i].get(j));

            }
        }

但后来我被困住了。输出如下:

0 [project2.Block@558fee4f, project2.Block@5c66b06b, project2.Block@59c87031, project2.Block@763dcf03, project2.Block@53e20a9a, project2.Block@1d262f7c, project2.Block@35f784d7, project2.Block@d325aef, project2.Block@64f007ad]
0 project2.Block@558fee4f
1 project2.Block@5c66b06b
2 project2.Block@59c87031
3 project2.Block@763dcf03
4 project2.Block@53e20a9a
5 project2.Block@1d262f7c
6 project2.Block@35f784d7
7 project2.Block@d325aef
8 project2.Block@64f007ad

但是如何进入块的元组以获取密钥?(获取key可以使用Tuple.key)

以下是 Block 的类

import java.util.ArrayList;

public class Block {
    /**
     * List of tuples contained in this block
     */
    protected ArrayList<Tuple> tupleLst; 

    public Block(){
        this.tupleLst=new ArrayList<Tuple>(Setting.blockFactor);
    }

    /**
     * Insert a tuple t to this block
     * @param t is a tuple to be inserted to this block
     * @return true if tuple t is successfully inserted into this block and false if the block is full
     */

    //One tupleLst can store up to "10" tuples
    public boolean insertTuple(Tuple t){
        if(tupleLst.size()<Setting.blockFactor){
            tupleLst.add(t);
            return true;
        }
        return false;
    }

    /**
     * @return number of tuples stored in this block
     */
    public int getNumTuples(){
        return tupleLst.size();
    }

    /**
     * Print this block 
     * @param printTuple is a flag to indicate whether the details of the tuples are printed 
     */
    public void print(boolean printTuple){
        System.out.println("[BlockSize: "+getNumTuples()+"]");
        if(printTuple){
            for(Tuple t:tupleLst){
                System.out.println(t);
            }
        }
    }
4

0 回答 0