954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A neater way to print elements in an array?

Is there a "neater" way to code the following? (It prints the 5 data elements in an array.)

System.out.println("All the values in the array are: ");
System.out.println(+ array1[0]);
System.out.println(+ array1[1]);
System.out.println(+ array1[2]);
System.out.println(+ array1[3]);
System.out.println(+ array1[4]);

Thanks,
Bud

Bud4java
Newbie Poster
13 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>Is there a "neater" way to code the following?

System.out.println ( "All the values in the array are: " );
for ( T x: array1 )
  System.out.println ( x );

Change T to whatever the type of the array is. Or, if you aren't up to the latest version of Java:

System.out.println ( "All the values in the array are: " );
for ( int i = 0; i < array1.length; i++ )
  System.out.println ( array1[i] );
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks...exactly what I was looking for!

Bud

Bud4java
Newbie Poster
13 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

a neater way of getting values from an array! Good work, buddy.

System.out.println ( "All the values in the array are: " );
for ( T x: array1 )
  System.out.println ( x );


Thanks.

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

@rotten69 was it worth to reopen 6 year old thread? (Created 02-05-05)

Thread closed, before another time-traveler arrival

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You