有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

共 (1) 个答案

  1. # 1 楼答案

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

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

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

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