...

View Full Version : Adding elements into a set of array lists



leiwenrei
11-17-2004, 03:04 AM
Hi i wonder if there's a more efficient way to do this in java MPI:


public void receiveBlock(int nproc){

int[][] blockData = new int[numCols];
MPI.COMM_WORLD.Recv(blockData, 0, block_size, MPI.Int, 0, 99);

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
ArrayList c = new ArrayList();

for (int i=0; i< block_size; i++){

int id = blockData[i][0];
int nodeID = id % nproc;

if (nodeID == 0)
a.add(blockData[i]);
else if (nodeID == 1)
b.add(blockData[i]);
else
c.add(blockData[i]);
}


int[][] arrayA = new int[a.size()][numCols];
arrayA = a.toArray();
int[][] arrayB = new int[b.size()][numCols];
arrayB = b.toArray();
int[][] arrayC = new int[c.size()][numCols];
arrayC = c.toArray();

// ...

}

basically, i have to hash the id field from an array and put those rows with the same hashed value into the same array. but i can't use arrays to do this as i can't determine how many rows each array will have before the hashing is complete. so i'm using arraylists to add the elements and then converting it into arrays. but this code seems so inefficient.
thanks for any help.

Put your code in some [ code ] tags. Try to use them in the future, they help out a lot. :) [B]Antoniohawk

leiwenrei
11-17-2004, 03:09 AM
Sorry, the bottom section of the code should be this:

int[][] arrayA = new int[a.size()][numCols];
arrayA = a.toArray();
int[][] arrayB = new int[b.size()][numCols];
arrayB = b.toArray();
int[][] arrayC = new int[c.size()][numCols];
arrayC = c.toArray();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum