[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

printing out array in Java

Posted on 2002-04-06
13
Medium Priority
?
436 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

650 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