JAVA Best way to create a string "*"

I am currently trying to create a line of 60 stars however I have found several ways to do this.

//Method 1
System.out.println(String.format("%60s", "").replace(' ', '*'));

//Method 2
for(int i=0;i<60;i++)
    System.out.print("*");
System.out.println("");

//Method 3
int i=0;
while(i<60) {
    System.out.print("*");
    i++;
}

What's the best fit? (In terms of time and style)

And if there are any other ways to do this?

//Modular Approach, not bad
static String repeat(final String str, final int n) {
    final int len = (str == null) ? 0 : str.length();
    if (len < 1 || n < 1) {
        return "";
    }
    final StringBuilder sb = new StringBuilder(len * n);
    for (int i = 0; i < n; i++) {
        sb.append(str);
    }
    return sb.toString();
}

System.out.println(repeat("*", 60));

AND

//Similar to method 1
System.out.println(new String(new char[60]).replace("\0", "*"));
+4
source share
8 answers

What about:

Stream.generate(() -> "*").limit(60).forEach(System.out::print);
System.out.println(); // for the newline at the end

Or slightly hacked, but one line:

System.out.println(new String(new char[60]).replace("\0", "*"));

The hacker version works because all java arrays of numeric primitives are initialized to 0 in all elements, which are then replaced with the desired character once per line.

+7
source

, , , , print . . (for while do-while) JIT ( ). . . String StringBuilder . - ,

static String repeat(final String str, final int n) {
    final int len = (str == null) ? 0 : str.length();
    if (len < 1 || n < 1) {
        return "";
    }
    final StringBuilder sb = new StringBuilder(len * n);
    for (int i = 0; i < n; i++) {
        sb.append(str);
    }
    return sb.toString();
}

System.out.println(repeat("*", 60));
+2

? :

int n=60;
char[] data = new char[60];

for(int i=0; i<n; i++)
    data[i] = '*';

System.out.print(data);

"".

+2

, System.out.println("************************************************************");

, , , ,

String sixtyStars = "************************************************************";

sixtyStars , .

+2

: O (n). M2 = M3 , M1 ( - String.format String.replace).

, M.2 .

M.2 , StringBuilder.

0

., , M1 , . M2 - . M4,

 PrintStart (60);\\ on call 


  \\function body 
 public static void PrintStart  ( int i){

       if (i==0)
           return; 
      System.out.print ("*");
      PrintStart  (i-1);
    }
0

collector?

String result = IntStream.range(0, 60).mapToObj(i -> '*')
        .collect(Collector.of(
                StringBuilder::new,
                StringBuilder::append,
                StringBuilder::append,
                StringBuilder::toString));

String result = IntStream.range(0, 60).mapToObj(i -> "*")
        .collect(Collectors.joining());
0

Here I used regularly - using Java 8 threads:

Stream.generate(() -> "*").limit(60).collect(Collectors.joining());

For me, he fixes the intention well: create a stream of stars, get the first 60 and combine them together.

0
source

Source: https://habr.com/ru/post/1615686/


All Articles