Java > Array-2 > fizzArray2 (CodingBat Solution)

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;
}


11 comments :

  1. public String[] fizzArray2(int n) {
    String[] result = new String[n];
    for(int i = 0; i < n; i++)
    {
    result[i] = String.valueOf(i);
    }
    return result;
    }

    ReplyDelete
  2. ```
    public String[] fizzArray2(int n) {
    return java.util.stream.IntStream.range(0, n)
    .boxed().map(String::valueOf).toArray(String[]::new);
    }
    ```

    ReplyDelete
  3. public String[] fizzArray2(int n) {

    String[] out = new String[n];
    int c = 0;

    for(int i = 0; i < n; i++){
    out[i] = Integer.toString(c++);
    } return out;
    }

    ReplyDelete
  4. public String[] fizzArray2(int n) {
    String[] s = new String[n];

    for(int i=0;i<n;i++) {
    s[i] = new Integer(i).toString();
    }

    return s;
    }

    ReplyDelete
  5. public String[] fizzArray2(int n) {
    String[] strArray = new String[n];
    Arrays.setAll(strArray, String::valueOf);
    return strArray;
    }

    ReplyDelete
  6. public String[] fizzArray2(int n) {
    String[] strArray = new String[n];
    Arrays.setAll(strArray, String::valueOf);
    return strArray;
    }

    ReplyDelete
  7. public String[] fizzArray2(int n) {
    String[] strArray = new String[n];
    Arrays.setAll(strArray, String::valueOf);
    return strArray;
    }

    ReplyDelete
  8. public String[] fizzArray2(int n) {
    String re[]=new String[n];
    for(int i=0;i<n;i++)
    {
    re[i]=""+i+"";
    }
    return re;
    }

    ReplyDelete
  9. public String[] fizzArray2(int n)
    {
    String[] newArray = new String[n];

    for(int i = 0; i < n; i++)
    {
    newArray[i] = String.valueOf(i);
    }

    return (newArray);
    }

    ReplyDelete

  10. with 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);

    }

    ReplyDelete
  11. public String[] fizzArray2(int n) {
    String[] a=new String[n];
    for(int i=0;i<n;i++)

    a[i]=(""+i+"");


    return a;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact