Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2007, 08:11 PM
Member
 
Join Date: Aug 2007
Posts: 1
pompeez is on a distinguished road
how to convert a Java array to a java stack?
Hi,
I would like to convert a Java Arraylist/array/list to an equivalent Java stack, such that the last element in the array becomes the forst element in the stack. The array I have is nested.
Thanks for your time!
Pompeez
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 03:37 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
The following code shows how to convert an array into a stack. If you have a List to start with simply skip step 1.
Code:
String[] stuff = {"this", "and", "that"}; List<String> list = Arrays.asList(stuff); //1 Convert to a List Stack<String> stack = new Stack<String>(); //2 Create new stack stack.addAll(list); //3 Add all items from the List to the stack.
Hope that sorts you out,
Shane.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 03:41 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
Oops I misread that you needed to reverse add it all so replace number 3 above with:
List
Code:
for(int i = list.size() - 1; i >= 0; i--) { stack.add(list.get(i)); }
Array
Remove step 1 as its not needed anymore.
Code:
for(int i = stuff.length - 1; i >= 0; i--) { stack.add(stuff[i]); }
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stack data structure in Java Java Tip java.lang 0 04-16-2008 11:34 PM
Demonstration of Stack class in java.util package Java Tip java.lang 0 04-16-2008 11:33 PM
How to use Stack class in java.util package Java Tip java.lang 0 04-16-2008 11:32 PM
Traversing through a stack of objects, and puttin them info in an array szimme101 New To Java 1 03-25-2008 06:06 AM
Using java.util.Stack Java Tip Java Tips 0 11-20-2007 06:17 PM


All times are GMT +3. The time now is 04:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org