printing out array in Java

How can I print out(std output) an entire array (of anything ) without actually looping through it ?
angrishAsked:
Who is Participating?
 
vogenConnect With a Mentor Commented:
//Try this:
import java.util.*;

String myCode[] = {"please", "try", "this", "code"};
ArrayList aList = new ArrayList(Arrays.asList(myCode));

System.out.println("Result: \n" + aList);


0
 
Peter KwanCommented:
If you don't loop through the entire array, how can you know what value of each element in the array to be printed out?
0
 
pellepCommented:
Try this


String anArray[] = {"test1", "test2", "test3"};
System.out.println(java.util.Arrays.asList(anArray));
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Peter KwanCommented:
asList can only work on Object array, but not an array of primitive type (e.g. int[]).
0
 
objectsCommented:
And technically speaking it (asList) still loops thru the array :-)
0
 
objectsCommented:
I think the short answer to your question is you can't.
0
 
LexZEUSCommented:
make your own Class derived from ArrayList, let said: MyArrayList, override add() methods to append into StringBuffer the object's toString(). When MyArrayList.toString() is called, return the StringBuffer's toString(). Cache the String if needed, coz I notice that calling StringBuffer.toString() will instantiated new String object.

just a brief view:

...
public class MyArrayList extends ArrayList
{
private StringBuffer sb = new StringBuffer(1000);
private String cache;
public void add(Object o)
  {
  super.add(o);
  sb.append(o.toString()).append(" , ");
  cache=null;
  }

public String toString()
  {
  if (cache==null) cache=sb.toString();
  return cache;
  }
...
}

0
 
Peter KwanCommented:
vogen,

What's your difference from pellep's comment?
0
 
vogenCommented:
// Well! according to java coding standard "try this"
// without comment mark will create error.
// kool!
0
 
pellepCommented:
vogen>>

You took almost the exact thing I said in my comment and proposed it as an answer. Please, do not lock a question (propose an answer) unless:
- You are 100% sure you propose a complete sollution to the exact problem stated in the question
- Your sollution is not already proposed by someone else.

This is for multiple reasons. Firstly, locking a question moves it to the 'locked' section of the topic area, limiting the chances of other experts noticing it and providing additionel comments that may be helpful to the questioner. Second, it annoys other experts a lot when people try to 'steal' their proposed sollutions and try to put them of as their own.

I realize you may be fairly new to this forum, so you may not be aware of the guidelines. You can read more about this on
http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp
and
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

under sections regarding guidelines on comments/answers.

Regards PAP
0
 
vogenCommented:
solly!! didn't notice that
0
 
angrishAuthor Commented:
Thanks All !!
0
 
objectsCommented:
a. that solution was a copy of a previous comment
b. it still loops thru your array
0
All Courses

From novice to tech pro — start learning today.