Solved

fizzArray  challenge

Posted on 2016-08-03
1
94 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 35

Accepted Solution

by:
mccarl earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 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