[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

printing out array in Java

Posted on 2002-04-06
13
Medium Priority
?
439 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 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