首页 > 解决方案 > 如何在二维字符数组的空单元格中插入占位符值

问题描述

我需要能够从文件中提取文本并将其插入到 2D 字符数组中,并且任何额外的单元格都需要用 @ 字符填充。如果文本文件太长,任何不适合的字符都需要忽略。我目前拥有的代码将文本放置在 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();
	    	
	    
	    }
		}
}

标签: javaarrays

解决方案


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

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

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

编辑:我添加了所有 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();
    }
}

推荐阅读