Forums Register Login

Beginners - would you like to do this question ?

+Pie Number of slices to send: Send
NOTE - Please post your solution here, even if someone has done it correctly before you.

Question - Make a method to print an array of integers in this format 3,7,8,9...

Thanks
1
+Pie Number of slices to send: Send
There you are. And even ******** ******** can’t do worse than that!
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:There you are. And even ******** ******** can’t do worse than that!



Different from what I had in mind - a method that accepts an int array and then prints it in the form - a0,a1,a2,a3....aN.


+Pie Number of slices to send: Send
Just goes to show... if you make up a question for beginners, you should write clear specifications. Otherwise you're going to have to argue with various people's interpretations of your unclear specifications.
+Pie Number of slices to send: Send
And what's the award for giving the answer you have in mind?
+Pie Number of slices to send: Send
 

Kemal Sokolovic wrote:And what's the award for giving the answer you have in mind?


No reward. Just want to look at the different solutions people come up with for the same problem.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:There you are. And even ******** ******** can’t do worse than that!


And if you use an Integer[], that last statement can be
System.out.printf("%d,%d,%d,%d...%n", numbers);

Winston
+Pie Number of slices to send: Send
My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.


I know. Just thought I'd inject a bit of education into the proceedings...

Winston
1
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.



How about this one then?



From the passive-aggressive school of programming.
+Pie Number of slices to send: Send
wow ! everyone but the beginners are here. Its turning into a meaningless drivel post.
+Pie Number of slices to send: Send
Would this work? o.o
+Pie Number of slices to send: Send
 

Johan Allis wrote:Would this work? o.o



The output is - 3,7,8,9,
I don't want a comma after the last element.
+Pie Number of slices to send: Send
Then this?
+Pie Number of slices to send: Send
Solution without an if:
+Pie Number of slices to send: Send
And what about the [...]? You haven’t printed any square brackets.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:How about this one then?

From the passive-aggressive school of programming.


Nice, and then of course there's:from the school of over-engineering.

Winston
+Pie Number of slices to send: Send
any more replies welcome. this thread is getting interesting.
+Pie Number of slices to send: Send
 

Andy Jack wrote:any more replies welcome. this thread is getting interesting.


Glad you're taking it in good humour. Some of us have nothing better to do with our time...

Winston
+Pie Number of slices to send: Send
 

Moe Kennedy wrote:Solution without an if:



********************************************************************

Johan Allis wrote:Then this?



********************************************************************

I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.

Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. Keeping my fingers crossed.


+Pie Number of slices to send: Send
 

Moe Kennedy wrote:Solution without an if:



********************************************************************

Johan Allis wrote:Then this?



********************************************************************

I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.

Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. I wonder if the interviewer is crazy. What purpose do questions like these serve ?
Keeping my fingers crossed.
+Pie Number of slices to send: Send
I thought I would try it properly, so I went to my Java folder and tried to create a class: gedit ArrayPrinter.java. Only an old class appeared, dated “Oct 19 14:54”. It looks like this:-

java ArrayPrinter 1 2 69 4
{1, 2, 69, 4}

It prints args, which is a String[] rather than an int[], but the principles are the same. Work out what line 10 means. It prints {} if you pass an empty args array.
1
+Pie Number of slices to send: Send
 

Andy Jack wrote:Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution.


Actually, Moe's solution also contains an inefficiency: it has to determine the result of array.length - 1 on each iteration. One alternative is:but my slight preference is:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.

Winston
+Pie Number of slices to send: Send
 

Winston Gutkowski wrote:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.


Johan's solution won't throw an exception for an empty array.
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1212 times.
Similar Threads
factory.openSession( new MyInterceptor() ): Sessionfactory not having api ,interceptor as parameter
SCJP 1.5 "In order to save memory ??" QUOTE
why are HTML checkboxes sometimes problematic for managing true/false values
How to say no?
JTable with hashmap--please help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:39:27.