Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

fizzArray  challenge

Posted on 2016-08-03
1
Medium Priority
?
163 Views
Last Modified: 2017-01-18
Hi,

I am working on below challenge

http://codingbat.com/prob/p180920

Psedo code description of approach :
1. identify size of array.
2. create new empty array of above size
3. keep putting each element of array with above values until size

I wrote my code as below



public int[] fizzArray(int n) {
		  int size=n;
		  int[] ar=new int[size];
		  for(int i=0;i<n;i++){
		    ar[i]=i;
		  }
		  
		  return ar;
		}

Open in new window


public class FizzArrayEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("fizzArray is-->"+fizzArray(4).toString());

	}

	
	public static int[] fizzArray(int n) {
		  int size=n;
		  int[] ar=new int[size];
		  for(int i=0;i<n;i++){
		    ar[i]=i;
		  }
		  
		  return ar;
		}

}

Open in new window

i got below output
fizzArray is-->[I@15db9742

I am not seeing any test results when i ran above code

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 41741687
The output you are seeing is the default way Java converts an Array to a String. What you might want to do is replace line 5 of the above code with this...

		System.out.println("fizzArray is-->"+Arrays.toString(fizzArray(4)));

Open in new window


It uses a separate utility class (java.util.Arrays) to convert the array (and it's contents) into a String for you.

BTW, I don't see anything wrong with your design or code, it works just fine in the codingbat site.

The only thing I would do differently is remove line 11, and just the variable n in line 12 (rather than size).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Introduction to Processes
Screencast - Getting to Know the Pipeline
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question