Reputation: 23
I'm supposed to be going through an array of these numbers, 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 then putting the first (non-repeating numbers) into a smaller array that holds only 5 of the numbers.
So after the first five are in, it would look like 7 0 1 2 3 (since 0 was already present within the array).
Then its supposed to search through and compare each element in the rest of the larger array with every element in the smaller one. T
The next element in the larger array is 0, the program needs to compare 0 to all the elements in the smaller array.
If the element exists in the smaller array, its just supposed to set a MRU variable equal to the index of the element that exists within the smaller array.
If the number doesn't exist, say like after the next run, 4. Then the program will replace the element at the MRU variable with the number 4.
I have two questions,
I've worked on this for many days and gone through countless variations. Its already passed the due date, but I want to learn how to do this.
import java.util.*;
import java.io.*;
public class MRUPageReplacement
{
public static void main(String [] args)
{
//======== Variables ==============================================
ArrayList<Integer>MRUList = new ArrayList<Integer>();
int [] frames = {7,0,1,2,3};
int i,j,MRU;
String line;
//======== File Reader ============================================
try
{
FileReader reader = new FileReader("MRU.txt");
BufferedReader r = new BufferedReader(reader);
while ((line=r.readLine())!=null)
{
MRUList.add(Integer.parseInt(line));
}
}
catch(Exception e)
{
System.out.println("File Not Found");
}
int[] array = new int [MRUList.size()];
for (i =0; i < MRUList.size(); i++)
{
array[i] = MRUList.get(i);
}
//======== Fill Arrays ==============================================
//======== Compare ==============================================
for(i=0; i<array.length; i++)
{ // Iterate through the array
for( j=0; j<frames.length; j++)
{ // Iterate through frames
if(array[i] == frames[j])
{
// if the element is in frames
MRU = j;
}
else {
// if the element is not in frames
frames[MRU] = array[i];
}
}
}
/*======== Print ==============================================
for(i=0; i<frames.length; i++)
{
System.out.println("frames : " + frames[i]);
}
*/
}
}
// Sample output
frames : 7
frames : 0
frames : 1
frames : 2
frames : 3
frames : 0
frames : 0
frames : 1
frames : 2
frames : 3
frames : 0
frames : 1
frames : 1
frames : 2
frames : 3
frames : 0
frames : 1
frames : 2
frames : 2
frames : 3
frames : 0
frames : 1
frames : 2
frames : 2
frames : 3
frames : 3
frames : 1
frames : 2
frames : 2
On a side note, when I try and print out the array instead of just the numbers it gives this: [I@565b540e. Is that because it's printing the index?
Eventually I would like to print out the frames array each time it runs through. Like: Run 1: Frames = {70123}.
EDIT: Ok so after some amazing help from Noctua, im now running into the main problem i was having before. It only recognizes the first or second iteration i can't tell since the second number is supposed to be a zero. Here is the part thats messing up:
for(i=0; i<array.length; i++)
{ // Iterate through Array
for( j=0; j<frames.length; j++)
{ // Iterate through Frames
if(array[i] == frames[j])
{
// Item from Array exists in Frames
MRU = j;
MRU_found = true;
}
}
if(!MRU_found)
{
frames[MRU] = array[i];
}
I've worked at it from a couple angles, however nothing seems to be working.
Upvotes: 2
Views: 1299
Reputation: 5218
for(i=0; i<array.length; i++) { // Iterate through the array
for( j=0; j<frames.length; j++) { // Iterate through frames
if(array[i] == frames[j]) { // if the element is in frames
MRU = j;
} else {
// if the element is not in frames
frames[MRU] = array[i];
}
}
}
Here's where's your mistake. In stead of searching the whole frames
array and then checking if you've met the frame, you've placed the else
-clause inside your loop.
What you probably meant, was something like:
for(i = 0; i < array.length; i++) {
for(j = 0; j < frames.length && !MRU_found; j++) {
if(array[i] == frames[j]) {
MRU = j;
MRU_found = true;
}
}
if(!MRU_found) {
frames[MRU] = array[i];
}
}
EDIT: On your side question, what you're printing is the address of the array in the memory.
To print the array each time, change the code to:
for(i = 0; i < array.length; i++) {
for(j = 0; j < frames.length && !MRU_found; j++) {
if(array[i] == frames[j]) {
MRU = j;
MRU_found = true;
}
}
if(!MRU_found) {
frames[MRU] = array[i];
}
System.out.print("frams: {");
for(j = 0; j < frames.length; j++) {
System.out.print(" ");
System.out.print(frames[j]);
}
System.out.println(" }");
}
Upvotes: 1