gpt4 book ai didi

java - 如何在二维字符数组的空单元格中插入占位符值

转载 作者:行者123 更新时间:2023-12-02 10:21:53 25 4
gpt4 key购买 nike

我需要能够从文件中提取文本并将其插入到二维字符数组中,并且任何额外的单元格都需要用 @ 字符填充。如果文本文件太长,则需要忽略任何不适合的字符。我当前的代码将文本放置在 20 行 x 45 列字符数组中,但前提是文本文件正好是 900 字节。

package myfirstjavaproject;

import java.io.*;
import java.util.Scanner;

public class temp {
public static void main(String[] args)throws Exception{
File file = new File ("test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st = br.readLine();

int row = 20, column = 45;
int offset = 0;
char[][] array = new char [row][column];

for (int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
array[i][j] = st.charAt(offset++);
System.out.print(array[i][j]);
}
System.out.println();


}
}
}

最佳答案

正如评论中提到的,一个简单的方法是首先用占位符填充面板,然后仅覆盖所需的位置。

另一种方法是使用您获得的偏移量来迭代数组的其余部分,并用占位符填充它。

第三种(可以说是更好的)方法是使用偏移量来限制对数组的访问(如果数组比实际文件大得多,这会明显更快)。

编辑:我添加了所有 3 种方法的代码示例

package basic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class temp {
private static final char PLACEHOLDER = '@';

public static void main(String[] args) throws Exception {
String input = "";
File file = new File("test.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
input = br.readLine();
}
int row = 20, column = 45;
char[][] array = new char[row][column];
// option 1
//firstFillWithPlaceholders(input, row, column, array);
// option 2
//firstFillWithData(input, row, column, array);
// print method for options 1 & 2
//printArray(row, column, array);
// option 3
My2DArray<Character> myClass = useOop(input, row, column);
// print method for option 3
System.out.println(myClass);
}

private static My2DArray<Character> useOop(String input, int row, int column) {
My2DArray<Character> result = new My2DArray<Character>(row, column, PLACEHOLDER);
int offset = 0;
for (int i = 0; i < row && offset < input.length(); i++) {
for (int j = 0; j < column && offset < input.length(); j++) {
result.set(i, j, input.charAt(offset++));
}
}
return result;
}

private static void firstFillWithData(String input, int row, int column, char[][] array) {
int offset = 0;
offset = writeData(input, row, column, offset, array);
fillTheRestWithPlaceholders(row, column, offset, array);
}

private static void fillTheRestWithPlaceholders(int row, int column, int offset, char[][] array) {
for (int i = offset / column; i < row; i++) {
for (int j = 0; j < column; j++) {
if (i*column + j >= offset) {
array[i][j] = PLACEHOLDER;
}
}
}
}

private static void firstFillWithPlaceholders(String input, int row, int column, char[][] array) {
int offset = 0;
fillWithPlaceHolders(row, column, array);
offset = writeData(input, row, column, offset, array);
}

private static void fillWithPlaceHolders(int row, int column, char[][] array) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
array[i][j] = PLACEHOLDER;
}
}
}

private static int writeData(String input, int row, int column, int offset, char[][] array) {
for (int i = 0; i < row && offset < input.length(); i++) {
for (int j = 0; j < column && offset < input.length(); j++) {
array[i][j] = input.charAt(offset++);
}
}
return offset;
}

private static void printArray(int row, int column, char[][] array) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
}

我的第三个选项使用新的“类”

package basic;

import java.util.HashMap;
import java.util.Map;

public class My2DArray<T> {

private final int row;
private final int column;
private final T placeholder;
private final boolean[][] isSet;
private final Map<Integer, T> data;

public My2DArray(int row, int column, T placeholder) {
this.row = row;
this.column = column;
this.placeholder = placeholder;
isSet = new boolean[row][column];
data = new HashMap<>();
}

public void set(int i, int j, T value) {
if (i < row && i >= 0 && j < column && j >= 0) {
isSet[i][j] = true;
data.put(i * column + j, value);
}
}

public T get(int i, int j) {
if (i < row && i >= 0 && j < column && j >= 0) {
if (isSet[i][j]) {
return data.get(i * column + j);
} else {
return placeholder;
}
} else {
throw new IndexOutOfBoundsException();
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
sb.append(get(i, j));
}
sb.append("\n");
}
return sb.toString();
}
}

关于java - 如何在二维字符数组的空单元格中插入占位符值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54284867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com