Printing string in rows and column pattern Java

前端 未结 7 2266
孤独总比滥情好
孤独总比滥情好 2021-02-07 14:31

i\'m just created a java project to print string that is given in rows and column just like matrix. Here\'s the output that i just made:

h e l l o 
_ w o r l 
d _         


        
7条回答
  •  醉酒成梦
    2021-02-07 15:17

    I think that the best way to implement this is the following:

    1. create an instruction object (Dictionary.java) which controls the fill-in process of the matrix
    2. fill in the matrix with data (Spiral.java)
    3. then show the matrix

    With this approach, you can change the pattern easily, without changing the rest of the code because the pattern generator works detached from the rest of the code.

    This is how the basic Dictionary class may look like:

    public abstract class Dictionary {
    
        protected int matrixSize;
        protected String[] dictionary;
    
        public Dictionary(int matrixSize) {
            this.matrixSize = matrixSize;
            dictionary = new String[matrixSize * matrixSize];
        }
    
        public abstract String[] createPattern();
    
        public void showPattern() {
            Arrays.stream(dictionary).forEach(System.out::println);
        }
    }
    

    For each pattern, you need to implement the createPattern() method differently. For example, a frame pattern implementation can be something like this:

    public class FrameDictionary extends Dictionary {
    
        protected int dictionaryIndex = 0;
        protected int startX, endX;
        protected int startY, endY;
    
        public FrameDictionary(int matrixSize) {
            super(matrixSize);
            startX = -1;
            endX = matrixSize - 1;
            startY = 0;
            endY = matrixSize - 1;
        }
    
        @Override
        public String[] createPattern() {
            while (dictionaryIndex < matrixSize) {
                pattern1();
                pattern2();
            }
            return dictionary;
        }
    
        /**
         * pattern 1
         * direction: left -> right then top -> bottom
         */
        protected void pattern1() {
            startX++;
            for (int i = startX; i <= endX; i++) {
                dictionary[dictionaryIndex] = i + ":" + startY;
                dictionaryIndex++;
            }
    
            startY++;
            for (int i = startY; i <= endY; i++) {
                dictionary[dictionaryIndex] = endX + ":" + i;
                dictionaryIndex++;
            }
        }
    
        /**
         * pattern 2
         * direction: right -> left then bottom -> top
         */
        protected void pattern2() {
            endX--;
            for (int i = endX; i >= startX; i--) {
                dictionary[dictionaryIndex] = i + ":" + endY;
                dictionaryIndex++;
            }
    
            endY--;
            for (int i = endY; i >= startY; i--) {
                dictionary[dictionaryIndex] = startX + ":" + i;
                dictionaryIndex++;
            }
        }
    }
    

    Output:

    a b c d e f
    t         g
    s         h
    r         i
    q         j
    p o n m l k
    

    You can draw the pattern what you need with the following implementation of the createPattern() method:

    public class ClockWiseDictionary extends FrameDictionary {
    
        public ClockWiseDictionary(int matrixSize) {
            super(matrixSize);
        }
    
        @Override
        public String[] createPattern() {
            int pixelsInMatrix = matrixSize * matrixSize;
            while (dictionaryIndex < pixelsInMatrix) {
                pattern1();
                pattern2();
            }
            return dictionary;
        }
    }
    

    Output:

    a b c d e f
    t u v w x g
    s 6 7 8 y h
    r 5 0 9 z i
    q 4 3 2 1 j
    p o n m l k
    

    Or just for fun, a "snake" pattern implementation:

    public class SnakeDictionary extends Dictionary {
    
        private int dictionaryIndex = 0;
        private int startY = 0;
    
        public SnakeDictionary(int matrixSize) {
            super(matrixSize);
        }
    
        @Override
        public String[] createPattern() {
            int pixelsInMatrix = matrixSize * matrixSize;
            while (dictionaryIndex < pixelsInMatrix) {
                pattern1();
                if (dictionaryIndex < pixelsInMatrix) {
                    pattern2();
                }
            }
    
            return dictionary;
        }
    
        public void pattern1() {
            for (int i = 0; i < matrixSize; i++) {
                dictionary[dictionaryIndex] = i + ":" + startY;
                dictionaryIndex++;
            }
            startY++;
        }
    
        public void pattern2() {
            for (int i = matrixSize - 1; i >= 0; i--) {
                dictionary[dictionaryIndex] = i + ":" + startY;
                dictionaryIndex++;
            }
            startY++;
        }
    }
    

    Output:

    a b c d e f
    l k j i h g
    m n o p q r
    x w v u t s
    y z 1 2 3 4
    0 9 8 7 6 5
    

    This is how the main method looks like:

    public static void main(String[] args) {
        String sentence = "abcdefghijklmnopqrstuvwxyz1234567890";
        String[][] spiral = new String[MATRIX_SIZE][MATRIX_SIZE];
    
        // Dictionary dictionary = new FrameDictionary(MATRIX_SIZE);
        Dictionary dictionary = new ClockWiseDictionary(MATRIX_SIZE);
        // Dictionary dictionary = new SnakeDictionary(MATRIX_SIZE);
    
        String[] pattern = dictionary.createPattern();
        //dictionary.showPattern();
    
        Spiral.fill(sentence, pattern, spiral);
        Spiral.show(spiral);
    }
    

    You can check/download the complete source code from GitHub.

    Hope that it helps you.

提交回复
热议问题