?
Solved

printing out array in Java

Posted on 2002-04-06
13
Medium Priority
?
432 Views
Last Modified: 2008-03-10
How can I print out(std output) an entire array (of anything ) without actually looping through it ?
0
Comment
Question by:angrish
[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
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6923433
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
 
LVL 4

Expert Comment

by:pellep
ID: 6923738
Try this


String anArray[] = {"test1", "test2", "test3"};
System.out.println(java.util.Arrays.asList(anArray));
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6923825
asList can only work on Object array, but not an array of primitive type (e.g. int[]).
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 92

Expert Comment

by:objects
ID: 6924271
And technically speaking it (asList) still loops thru the array :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6924275
I think the short answer to your question is you can't.
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6925233
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
 
LVL 2

Accepted Solution

by:
vogen earned 300 total points
ID: 6925548
//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
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6925656
vogen,

What's your difference from pellep's comment?
0
 
LVL 2

Expert Comment

by:vogen
ID: 6925760
// Well! according to java coding standard "try this"
// without comment mark will create error.
// kool!
0
 
LVL 4

Expert Comment

by:pellep
ID: 6925814
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
 
LVL 2

Expert Comment

by:vogen
ID: 6927435
solly!! didn't notice that
0
 

Author Comment

by:angrish
ID: 6930156
Thanks All !!
0
 
LVL 92

Expert Comment

by:objects
ID: 6930167
a. that solution was a copy of a previous comment
b. it still loops thru your array
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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