我应该通过这些数字的数组,7,0,2,0,0,4,3,0,3,2,2,0,1,7,0 1,然后把第一个(不重复的数字)放到一个小数组中,这个数组只包含5个数字。
因此,在前五种情况出现后,它看起来就像是7 0 1 2 3(因为0已经存在于数组中)。
然后,它应该搜索并比较大数组其余部分中的每个元素和较小数组中的每个元素。T
大数组中的下一个元素是0,程序需要将0与较小数组中的所有元素进行比较。
如果元素存在于较小的数组中,那么它应该设置一个MRU变量,该变量等于小数组中存在的元素的索引。
如果这个数字不存在,比如说在下一次运行之后,4,那么程序将用数字4替换MRU变量中的元素。
我有两个问题,
我在这方面做了很多天,经历了无数的变化。它已经过了到期日,但我想学习如何做到这一点。
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另外,当我尝试打印数组而不是仅仅打印数字时,它给出的是:[I@565b540e ]。是因为它在打印索引吗?
最后,我想打印出帧数组,每次它运行。类似:运行1: Frame= {70123}。
编辑:好的,在得到夜夜的帮助后,我现在遇到了我之前遇到的主要问题。它只识别第一次或第二次迭代,因为第二个数字应该是零。以下是搞糟的部分:
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];
}我从几个角度做过这方面的工作,但是似乎没有什么效果。
发布于 2012-11-30 21:39:32
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];
}
}
}这是你的错误所在。不是搜索整个frames数组,然后检查是否遇到了该框架,而是在循环中放置了else-clause。
你的意思大概是:
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];
}
}编辑:在你旁边的问题上,你打印的是内存中数组的地址。
若要每次打印数组,请将代码更改为:
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(" }");
}https://stackoverflow.com/questions/13653412
复制相似问题