arrays help

hi,

im trying to write a program to represent a memory and a cache

i have written a class for the memory containing a 2d array holding random integers - i will post the code for this below for you to gain an understanding

i now wish to write a cache class which contains 4 2d arrays

32x1 for validity bit (0 if the corresponding cache block is empty, 1 if it has stuff in)

32x1 for index

32x1 for tag

32x8 for the actual cache blocks

however, i am unsure of how to go about doing this

if anybody could offer any help or psuedo code id appreciate it very much

thanks

the code for the memory class is as follows:

import java.util.Random;

publicclass Memory

{

publicstaticvoid main(String args[])

{

int[][] memoryArray=newint[50][8];

Random randnum =new Random();

for(int i=0;i<50;i++)

{

for(int j=0;j<8;j++)

{

memoryArray[i][j]=randnum.nextInt(99);

}

}

for(int i=0;i<50;i++)

{

System.out.print("["+i+"] ");

for(int j=0;j<8;j++)

{

System.out.print(memoryArray[i][j] +" ");

}

System.out.println();

}

}

}

[2222 byte] By [johnnysmitha] at [2007-11-27 3:42:57]
# 1
What exactly are you having trouble with? If you know how to create 2d arrays, then just make 4 of them. The 32 x 1 arrays don't need to be 2d, since there's only 1 element in the second dimension.
hunter9000a at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 2

well the problem was how exactly i would write them in the class

but from my understanding the class should look like

public class whatever

{

array1

array2

array3

array4

}

one problem however, i know this is a very basic question

as you say it only needs to be a 1d array if the second dimension is 1

how would i write this?

thanks

johnnysmitha at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 3

Here's a start. Declare your arrays as class members, then initialize them in the constructor.

public class Cache {

private boolean[] validity;

private int[][] cacheBlocks;

// etc...

public Cache() {

// initialize the arrays

}

}

hunter9000a at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 4
thanksagain, another basic questionwhat do you mean by initialize?thanks
johnnysmitha at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 5

You're welcome. Initialize means assign a value to it. In your original code this is the initialization part:

int[][] memoryArray=new int[50][8];// create the array object with 50*8 elements

for(int i=0;i<50;i++)

{

for(int j=0;j<8;j++)

{

memoryArray[i][j]=randnum.nextInt(99);// assign a value to each of those elements

}

}

hunter9000a at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 6
cheersinstead of having a boolean array, would it be possible to write an if statement to return 0 if the corresponding cache block is empty and 1 if it has stuff in it?where would i place the if statement?thanks
johnnysmitha at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...
# 7

So you have 32 cache blocks, and one boolean in the array for each one. If you want to check whether block 27 is valid, then examine the 27th boolean value in the validity array, and return that. Your method for determining if a cache block is empty takes an int parameter, and returns a boolean that describes if that cache block is valid or not.

hunter9000a at 2007-7-12 8:46:34 > top of Java-index,Java Essentials,New To Java...