An interesting example of note generation list random numbers formatted

2019-06-25

Keywords: two-dimensional array list, generate rows of the array


 

1, demand

 

Sometimes we may have a need to generate a long list of demands strict format printing array. Under this type of example, FIG.

Although the arrangement of the data whether the format correctness and efficiency will not affect running. But a fresh layout often can bring very good reading experience, many programmers when writing such arrays will look at the situation layout data.

 

When a small amount of data can handmade layout, a data amount is large to make it possible to have the program automatically generated. When the array generated automatically by the program, most people tend to increase when printing by a tab '\ t' can be a little in order to align the number. But in fact, the alignment of the '\ t' tab is actually very poor in.

 

Is there a way that can generate an absolute neat typography version of it?

 

Have! Today I wrote such a small program out. The amount of code not many students in need of Tieshanglai hereby refer to the study.

 

2, to achieve

 

First of all I need to say:

I need is to generate a two-dimensional array by a specified number of rows of random numbers and the number of columns. And typeset in the form of text shown in the first picture. 

For example, I need a 32 row 4, a two-dimensional random array of numerical ranges between 0 to 64.

 

We need to know how to achieve it? The complete code at the end of the text.

 

First, let's look at the text of the first sample image, we can see it every line is there are strict laws.

 

Then we can "line" to start with, each row of data is ready, and then print it out.

 

Meanwhile, in order to have the same effect typesetting in any text editor, spacing between characters unify the space character instead of a tab character.

 

So, the first step , we first define a method for encapsulating this functionality.

static void generator(final int row, final int range) {
    
}

We need to specify the value of this line and two-dimensional array of dynamic range. Note that, because the author is using the byte array to represent a line of data, byte array can not be variable length specified at instantiation. Therefore, in this example, the author of a two-dimensional array of n rows and four columns as an example. If students really need to dynamically set the number of columns, you can be represented by a collection of the data line.

 

The second step , we define a byte array, used to represent his party.

 

I side of the range in the range 0 to 255, each digital value to count the number of bits is 3, the length of the longest line will be in the form of the form

{ 255,  255,  255,  255, }, //31

Therefore, we can be defined as a byte array of the form

    static void generator(final int row, final int column, final int range) {
        byte[] line = new byte[40];
        
        line[0] = '{';
        line[1] = ' ';
        line[31] = ' ';
        line[32] = '}';
        line[33] = ',';
        line[34] = ' ';
        line[35] = '/';
        line[36] = '/';
        line[39] = '\n';
    }

Here, we have some fixed characters to fill out. When the back and then generating a random number corresponding random number bits pit padding on it.

 

The third step , we need to define a StringBuilder added to each row of data and define a random data generation instance. It is defined to increase as shown below.

StringBuilder sb = new StringBuilder();
Random rd = new Random();

 

The fourth step , we can do our core algorithm of. Ado, directly attached to the code.

        int randomValue = -1 ;
         int POS = 0 ;
         for ( int I = 0; I <Row; I ++ ) {
             for ( int J = 0; J <. 4; J ++ ) { 
                randomValue = rd.nextInt (Range); 
                
                POS = * 2. 6 + J ; // start position POS is the subscript for each column in the previous byte array. 
          // next few if is to be converted into digital characters can be printed out directly.
IF (randomValue <10 ) { Line [POS] = ( byte ) (randomValue + '0' ); Line [POS +. 1] = ',' ; line[pos + 2] = ' '; line[pos + 3] = ' '; line[pos + 4] = ' '; line[pos + 5] = ' '; }else if(randomValue < 100) { line[pos] = (byte) (randomValue / 10 + '0'); line[pos + 1] = (byte) (randomValue % 10 + '0'); line[pos + 2] = ','; line[pos + 3] = ' '; line[pos + 4] = ' '; line[pos + 5] = ' '; }else { line[pos] = (byte) (randomValue / 100 + '0'); line[pos + 1] = (byte) (randomValue % 100 / 10 + '0'); line[pos + 2] = (byte) (randomValue % 100 % 10 + '0'); line[pos + 3] = ','; line[pos + 4] = ' '; line[pos + 5] = ' '; } // This is added to every several lines a coordinate annotation, indicating the current row is the first few lines. But also to enhance the reading experience. IF (I. 3% == 0 ) { IF (I <10 ) { Line [ 35] = '/' ; Line [ 36] = '/' ; Line [ 37 [] = ( byte ) (I + '0' ) ; Line [ 38 is] = '' ; } the else IF (I <100 ) { Line [ 35] = '/' ; Line [ 36] = '/' line[37] = (byte) (i / 10 + '0'); line[38] = (byte) (i % 10 + '0'); }else { line[35] = '/'; line[36] = '/'; } }else { line[35] = ' '; line[36] = ' '; line[37] = ' '; line[38] = ' '; } }// inner for -- end. // delete the last ',' if(i == row - 1) { line[33] = ' '; } sb.append(new String(line, 0, line.length)); }

 

Finally, the fifth step , nothing happened, directly form the previous sb.toString () is printed out of the data we want.

System.out.println(sb.toString());

 

I tried here, to generate a two-dimensional array of 13 rows and four columns of number, results as shown below

 

 

 

You're done, copy directly to where they need to use it.

 

Here posted the complete code.

import java.util.Random;

public class TG {

    public static void main(String[] args) {
        generator(13, 255);
    }
    
    static void generator(final int row, final int range) {
        byte[] line = new byte[40];
        
        line[0] = '{';
        line[1] = ' ';
        line[31] = ' ';
        line[32] = '}';
        line[33] = ',';
        line[34] = ' ';
        line[35] = '/';
        line[36] = '/';
        line[39] = '\n';
        
        StringBuilder sb = new StringBuilder();
        Random rd = new Random();
        
        int randomValue = -1;
        int pos = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < 4; j++) {
                randomValue = rd.nextInt(range);
                
                pos = j * 6 + 2;
                if(randomValue < 10) {
                    line[pos] = (byte) (randomValue + '0');
                    line[pos + 1] = ',';
                    line[pos + 2] = ' ';
                    line[pos + 3] = ' ';
                    line[pos + 4] = ' ';
                    line[pos + 5] = ' ';
                }else if(randomValue < 100) {
                    line[pos] = (byte) (randomValue / 10 + '0');
                    line[pos + 1] = (byte) (randomValue % 10 + '0');
                    line[pos + 2] = ',';
                    line[pos + 3] = ' ';
                    line[pos + 4] = ' ';
                    line[pos + 5] = ' ';
                }else {
                    line[pos] = (byte) (randomValue / 100 + '0');
                    line[pos + 1] = (byte) (randomValue % 100 / 10 + '0');
                    line[pos + 2] = (byte) (randomValue % 100 % 10 + '0');
                    line[pos + 3] = ',';
                    line[pos + 4] = ' ';
                    line[pos + 5] = ' ';
                }
                
                // comment in tail.
                if(i % 3 == 0) {
                    if(i < 10) {
                        line[35] = '/';
                        line[36] = '/';
                        line[37] = (byte) (i + '0');
                        line[38] = ' ';
                    }else if(i < 100) {
                        line[35] = '/';
                        line[36] = '/';
                        line[37] = (byte) (i / 10 + '0');
                        line[38] = (byte) (i % 10 + '0');
                    }else {
                        line[35] = '/';
                        line[36] = '/';
                    }
                }else {
                    line[35] = ' ';
                    line[36] = ' ';
                    line[37] = ' ';
                    line[38] = ' ';
                }
                    
            }// inner for -- end.
            
            // delete the last ','
            if(i == row - 1) {
                line[33] = ' ';
            }
            sb.append(new String(line, 0, line.length));
        }
        
        System.out.println(sb.toString());
    }

}
完整代码

 

对了,可以看到我所生成的二维数组中,每一行最后一列还是带有一个逗号分隔符。其实,为了美观,这个逗号分隔符最好不要出现。有兴趣的同学可以自行修改源码来实现这个需求啦。

 


 

Guess you like

Origin www.cnblogs.com/chorm590/p/11082708.html