简体   繁体   English

在 Java 中访问数组外循环的问题

[英]Issue with accessing array outside loop in Java

EDIT: I took a different approach, and it now works!编辑:我采取了不同的方法,现在可以了! Thanks to those who helped!感谢帮助过的人!

I'm trying to write a program that simulates an assembler.我正在尝试编写一个模拟汇编程序的程序。 I was able to read in the instruction set (see below) as well as a sample line from the code that will be assembled.我能够阅读指令集(见下文)以及将要组装的代码中的示例行。 I was also able to separate the components of each file into different arrays.我还能够将每个文件的组件分成不同的 arrays。 However, whenever I try to print out the arrays outside of the loops that populated them or just accessing them in general, I'm suddenly not able to get the right results.但是,每当我尝试在填充它们的循环之外打印出 arrays 或只是一般地访问它们时,我突然无法获得正确的结果。

public class Assembler {

 public static String[] data;
 public static String[] commands;
 public static String[] bin;
 
 public static String[] data2;
 public static String[] opCode;
 public static String[] operand;
 
  
 public static void main(String[] args) {
  try {
   File input = new File("mac1.txt");
   Scanner in = new Scanner(input);
   
   /*
    * Parse mac1 text file
    */ 
   while(in.hasNextLine()) {
     String line = in.nextLine(); //reads the text file one line at a time
     data = line.split("\\|"); //delimits the data by "|" and stores it in array data[]
   
   //assigns size to the arrays that will hold the mnemonics and binary
   commands = new String[data.length - 1];
   bin = new String[data.length - 1];
   
   //populates the arrays with corresponding data then displays them
   for(int i = 0; i < bin.length; i++)
   {
     commands[i] = data[i];
     bin[i] = data[i + 1]; 
     System.out.println(commands[i] + "  " + bin[i]);
   }
  }
   
   File input2 = new File("algo.txt");
   Scanner in2 = new Scanner(input2);
   
   
   System.out.println("--------------------------------");
   while(in2.hasNextLine()) {
     String line2 = in2.nextLine(); //reads the text file one line at a time
     data2 = line2.split(" "); //delimits the data by "|" and stores it in array data[]

   //assigns size to the arrays that will hold the mnemonics and binary
   opCode = new String[data2.length - 1];
   operand = new String[data2.length - 1];
   
   //populates the arrays with corresponding data then displays them
   for(int i = 0; i < opCode.length; i++)
   {
     opCode[i] = data2[i];
     operand[i] = data2[i + 1]; 
     System.out.println(opCode[i] + "  " + operand[i]);
   }
  }
   
   /*
    * Outputs only the last element = 11111110 
    */ 
   for(int i = 0; i < bin.length; i++)
   {
     System.out.println(bin[i]);
   }
   
   translate(bin, commands);
   in.close();

  }
  catch(IOException e) {
   System.out.println("File not found!"); //displays error msg if file is not found
  }
 }
 
     /*
      * Outputs only the last element = 11111110 
      */ 
     public static void translate(String[] bin, String[] commands)
     {
       System.out.println(Arrays.toString(bin));
     }
    }

Input:输入:

在此处输入图像描述

Output: Output:

在此处输入图像描述

I guess my main question is how would I be able to access the arrays outside the loops as well as in other methods later down the line?我想我的主要问题是我如何能够在循环之外以及以后的其他方法中访问 arrays? It only prints out the last element in the array when using toString() outside the loops.它仅在循环外使用 toString() 时打印出数组中的最后一个元素。 The objective is to take a program written in this specific language and compose a binary output by means of translating.目标是获取一个用这种特定语言编写的程序,并通过翻译组成一个二进制 output。

Thanks!谢谢!

You should not create new instances for the arrays inside the loop …您不应该在循环内为 arrays 创建新实例……

With your current setup, each line overwrites the previous one;使用您当前的设置,每一行都会覆盖前一行; you do not see that because you dump the input from inside the read loop.您看不到这一点,因为您从读取循环内部转储了输入。

Next you should consider to define a struct (a class in Java) for the contents of each line and store that to an instance of List that you convert to an array after you read all lines.接下来,您应该考虑为每行的内容定义一个结构(Java 中的 class),并将其存储到List的实例中,然后在读取所有行后将其转换为数组。 Alternatively you define an array outside the loop that simulates the system memory;或者,您可以在模拟系统 memory 的循环定义一个数组; obviously, that array needs to be large enough to take most input files.显然,该数组需要足够大以容纳大多数输入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM