Solved

printing out array in Java

Posted on 2002-04-06
13
425 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Accepted Solution

by:
vogen earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 56
JList custom Cell Renderer refresh 15 53
ForLoop Example 3 40
MySQL  on Tomcat 8 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now