将文本文件拆分为两个并行数组 (java)
Splitting a text file into two parallel arrays (java)
我有一个文件,它是将莫尔斯电码模式翻译成字母表。
我需要将这个文件分成两个单独的数组中的键和值。
我希望有人能告诉我可以生成类似结果的基本算法,这样我就可以根据它来建模我的代码。
How would I split the left section into it's own array as well as the
right section into its own array? [1]:
https://i.stack.imgur.com/X3i99.png
读入文件,然后逐个字符地将它们与您构造的可能字符数组进行比较。构造另外两个数组,如果比较匹配一个或另一个,则根据比较将字符放入正确的数组中。
// Java program to iterate over an array
// using for loop
import java.io.*;
class GFG {
public static void main(String args[]) throws IOException
{
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int i, x;
// iterating over an array
for (i = 0; i < ar.length; i++) {
// accessing each element of array
x = ar[i];
System.out.print(x + " ");
}
}
}
读取类似
的文件
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
我有一个文件,它是将莫尔斯电码模式翻译成字母表。
我需要将这个文件分成两个单独的数组中的键和值。
我希望有人能告诉我可以生成类似结果的基本算法,这样我就可以根据它来建模我的代码。
How would I split the left section into it's own array as well as the right section into its own array? [1]: https://i.stack.imgur.com/X3i99.png
读入文件,然后逐个字符地将它们与您构造的可能字符数组进行比较。构造另外两个数组,如果比较匹配一个或另一个,则根据比较将字符放入正确的数组中。
// Java program to iterate over an array
// using for loop
import java.io.*;
class GFG {
public static void main(String args[]) throws IOException
{
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int i, x;
// iterating over an array
for (i = 0; i < ar.length; i++) {
// accessing each element of array
x = ar[i];
System.out.print(x + " ");
}
}
}
读取类似
的文件import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}