Problem:
Given a number n, create and return a new string array of length n, containing the strings "0", "1" "2" .. through n-1. N may be 0, in which case just return a length 0 array. Note: String.valueOf(xxx) will make the String form of most types. The syntax to make a new string array is: new String[desired_length] (See also: FizzBuzz Code)
fizzArray2(4) → {"0", "1", "2", "3"}
fizzArray2(10) → {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
fizzArray2(2) → {"0", "1"}
Solution:
public String[] fizzArray2(int n) { String[] result = new String[n]; for (int i = 0; i < n; i++) result[i] = String.valueOf(i); return result; }
public String[] fizzArray2(int n) {
ReplyDeleteString[] result = new String[n];
for(int i = 0; i < n; i++)
{
result[i] = String.valueOf(i);
}
return result;
}
```
ReplyDeletepublic String[] fizzArray2(int n) {
return java.util.stream.IntStream.range(0, n)
.boxed().map(String::valueOf).toArray(String[]::new);
}
```
public String[] fizzArray2(int n) {
ReplyDeleteString[] out = new String[n];
int c = 0;
for(int i = 0; i < n; i++){
out[i] = Integer.toString(c++);
} return out;
}
public String[] fizzArray2(int n) {
ReplyDeleteString[] s = new String[n];
for(int i=0;i<n;i++) {
s[i] = new Integer(i).toString();
}
return s;
}
public String[] fizzArray2(int n) {
ReplyDeleteString[] strArray = new String[n];
Arrays.setAll(strArray, String::valueOf);
return strArray;
}
public String[] fizzArray2(int n) {
ReplyDeleteString[] strArray = new String[n];
Arrays.setAll(strArray, String::valueOf);
return strArray;
}
public String[] fizzArray2(int n) {
ReplyDeleteString[] strArray = new String[n];
Arrays.setAll(strArray, String::valueOf);
return strArray;
}
public String[] fizzArray2(int n) {
ReplyDeleteString re[]=new String[n];
for(int i=0;i<n;i++)
{
re[i]=""+i+"";
}
return re;
}
public String[] fizzArray2(int n)
ReplyDelete{
String[] newArray = new String[n];
for(int i = 0; i < n; i++)
{
newArray[i] = String.valueOf(i);
}
return (newArray);
}
ReplyDeletewith Java Stream
public static String[] fizzArray2(int n) {
int[] arr = IntStream.range(0, n).toArray();
return Arrays.stream(arr)
.mapToObj(String::valueOf)
.toArray(String[]::new);
}
public String[] fizzArray2(int n) {
ReplyDeleteString[] a=new String[n];
for(int i=0;i<n;i++)
a[i]=(""+i+"");
return a;
}