Solved

printing out array in Java

Posted on 2002-04-06
13
424 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

759 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

26 Experts available now in Live!

Get 1:1 Help Now