使用文本文件中的哈希表表示内存

Representing memory using hashtable from a text file

我有一个项目,需要在 Java 中创建一个内存管理程序,我有文本文件,但不知道从哪里开始。我已经熟悉 Java 中的 split() 方法,但基本上我希望索引作为键,后面的所有内容作为值。

这是文本文件,因为它是大 AF:https://pastebin.com/Q4whQHxW

代码:


String[] temp;
temp = data.split("\n"); //assume data is the string that contains the text 
//doc contents
String[][] temp2 = new String[temp.length][];

for(int i = 0; i < temp.length; i++) {
    temp2[i] = temp[i].split("( )");
}
for(int i = 0; i < temp2.length; i++) {
    for(int j = 1; j < temp2[i].length; j++)
        memoryPhysical.put(Integer.parseInt(temp2[i][0]),temp2[i][j]);
}
System.out.println(memoryPhysical);

您可以使用 Map 作为键值对。如果您对使用 Map 有任何限制,请告诉我写一个数组示例。下面的代码向您展示了如何使用地图:

import java.util.Arrays;
import java.util.HashMap;

public class App {
    public static void main(String[] args) {

        String text = "0 96\n" +
                "1 29\n" +
                "2 304\n" +
                "3 561\n" +
                "4 742\n" +
                "5 621\n" +
                "6 620\n" +
                "11605 sub %14922, 12566\n" +
                "11606 mov %15653, %12958";

        HashMap<Integer, String> items = new HashMap<>();

        Arrays.stream(text.split("\n")).forEach((s) -> {
            int splitIndex = s.indexOf(" ");
            String key = s.substring(0, splitIndex);
            String value = s.substring(splitIndex);

            items.put(Integer.parseInt(key), value);
        });

        System.out.println(items);
    }
}

输出:

{0= 96, 1= 29, 2= 304, 3= 561, 4= 742, 5= 621, 11605= sub %14922, 12566, 6= 620, 11606= mov %15653, %12958 }