Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在二维字符数组的空单元格中插入占位符值_Java_Arrays - Fatal编程技术网

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

Java 如何在二维字符数组的空单元格中插入占位符值,java,arrays,Java,Arrays,我需要能够从文件中提取文本并将其插入到二维字符数组中,任何额外的单元格都需要填充@字符。如果文本文件太长,则需要忽略任何不适合的字符。我目前的代码将文本放置在一个20行45列的字符数组中,但前提是文本文件正好是900字节 包myfirstjavaproject; 导入java.io.*; 导入java.util.Scanner; 公共类临时工{ 公共静态void main(字符串[]args)引发异常{ File File=新文件(“test.txt”); BufferedReader br=

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

包myfirstjavaproject;
导入java.io.*;
导入java.util.Scanner;
公共类临时工{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“test.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
字符串st=br.readLine();
int行=20,列=45;
整数偏移=0;
字符[][]数组=新字符[行][列];
对于(int i=0;i}
如前所述,一个简单的方法是首先用占位符填充黑板,然后只覆盖所需的位置

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

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

编辑:我已经添加了所有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;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
公共类临时工{
私有静态最终字符占位符='@';
公共静态void main(字符串[]args)引发异常{
字符串输入=”;
File File=新文件(“test.txt”);
try(BufferedReader br=new BufferedReader(new FileReader(file))){
输入=br.readLine();
}
int行=20,列=45;
字符[][]数组=新字符[行][列];
//选择1
//FirstFillWithPlaceholder(输入、行、列、数组);
//选择2
//firstFillWithData(输入、行、列、数组);
//选项1和2的打印方法
//打印数组(行、列、数组);
//选择3
My2DArray myClass=useOop(输入、行、列);
//选项3的打印方法
System.out.println(myClass);
}
私有静态My2DArray useOop(字符串输入、int行、int列){
My2DArray结果=新的My2DArray(行、列、占位符);
整数偏移=0;
对于(int i=0;i=偏移量){
数组[i][j]=占位符;
}
}
}
}
私有静态void firstFillWithPlaceholder(字符串输入、int行、int列、char[][]数组){
整数偏移=0;
FillWithPlaceholder(行、列、数组);
偏移量=写入数据(输入、行、列、偏移量、数组);
}
私有静态void fillWithPlaceholder(int行、int列、char[][]数组){
对于(int i=0;i
我的第三个选项使用了一个新的“类”

package-basic;
导入java.util.HashMap;
导入java.util.Map;
公共类My2DArray{
私人最终int行;
私有的最后一个int列;
专用最终T占位符;
私有最终布尔值[][]isSet;
私人最终地图数据;
公共My2DArray(int行、int列、T占位符){
this.row=行;
this.column=列;
this.placeholder=占位符;
isSet=新布尔值[行][列];
data=newhashmap();
}
公共无效集(int i,int j,T值){
如果(i=0&&j=0){
isSet[i][j]=真;
数据输入(i*列+j,值);
}
}
公共T-get(int i,int j){
如果(i=0&&j=0){
if(isSet[i][j]){
返回数据.get(i*列+j);
}否则{
返回占位符;
}
}否则{
抛出新的IndexOutOfBoundsException();
}
}
@凌驾
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
首先,创建您的阵列
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();
    }
}