Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

fizzArray  challenge

Posted on 2016-08-03
1
Medium Priority
?
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

721 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