Modification des éléments d'un tableau
Bonjour,
J'essaie de modifier un élément d'un tableau, mais lorsque je fais ça, tous les éléments de mon tableau se retrouvent modifiés :
La classe où je déclare la tableau :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public class Memory {
private enum BlockState
{
FREE, IN_READING, IN_WRITING, WRITTEN;
}
/** All the blocks */
private Block[] blocks;
/**
* Default constructor
* Does nothing...
*/
public Memory()
{
blocks = new Block[13981];
initializeMemory();
}
private void initializeMemory()
{
Block b = new Block();
for(int i=0;i<blocks.length;i++)
{
blocks[i] = b;
}
} |
Avec la classe Block qui est une private class dans Memory :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
private class Block
{
/** Block state */
private BlockState blockState;
/** Image Id */
private int imageId;
/** Start date */
Calendar startDate;
/** End date */
Calendar endDate;
/**
* Default constructor
* Block State set to FREE
* Image Id set to 0
*/
public Block()
{
blockState = BlockState.FREE;
imageId = -1;
startDate = null;
endDate = null;
}
} |
et dans la classe Memory, les fonctions suivantes (en résumé) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public void writeBlocks(int startIdx, int nbBlocks, arg...)
{
for (int i=startIdx;i<startIdx+nbBlocks;i++)
{
writeOneBlock(i, arg...);
}
}
public void writeOneBlock(int ind, arg...)
{
// Block State
blocks[ind].blockState = BlockState.IN_WRITING;
} |
Cette dernière fonction writeOneBlock modifie l'état de tous les indices du tableau à "IN_WRITING". Pourquoi ???
Si j'ai oublié de préciser des trucs, n'hésitez pas. Merci !