• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

printing out array in Java

How can I print out(std output) an entire array (of anything ) without actually looping through it ?
0
angrish
Asked:
angrish
  • 3
  • 3
  • 3
  • +3
1 Solution
 
Peter KwanAnalyst ProgrammerCommented:
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
 
Peter KwanAnalyst ProgrammerCommented:
asList can only work on Object array, but not an array of primitive type (e.g. int[]).
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
vogenCommented:
//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 KwanAnalyst ProgrammerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 3
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now