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 01-03-2008, 11:22 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Bounded Array
We can declare an ArrayList bounded by String as shown below:

Code:
ArrayList<String> arrayList = new ArrayList<String>();
Can we also declare a simple array bounded by String or some other datatype? Please write syntax.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-03-2008, 11:36 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 291
tim is on a distinguished road
Hello bugger.

I think it's just the normal declaration of an array of type String:

Code:
int size = 10; String[] array = new String[size];
or in Synax:

Code:
int size = 10; <type here>[] array = new <type here>[size];
Hope this helps.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-03-2008, 12:56 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
If you are concerned on including the Generics things as well, make it like

Code:
int size = 10 ; ArrayList<Object> array = new ArrayList<Object>[size] ;
If your elements in array are not of same type then you need to cast them to their original type before using them. instanceof operator can be useful in this case.
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-04-2008, 05:27 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Whenever i want to have a group of objects of a type other than String or ints or doubles, i use ArrayList. It's better in the sense that it doesn't have a fixed length. When you have an array there can only be a certain number of objects in that array, but in an ArrayList you can add as many as you'd like.

I also like using HashSet when order doesn't matter, and when you want it to not repeat elements. A while ago i started on my first real test of my logic skills: a Sudoku game, and, my knowledge didn't really offer a solution to how i could check if the user won. But, when i learned about HashSets it completely solved my problem. Basically what i did was, add all the numbers (of a row, column, or square) the user entered into the HashSet, and if the size of the HashSet is less than 9, they didn't win (there's a method to check the rows, colums, and squares). The reason for this is, if there are not 9 number in the HashSet, that means that the user repeated a number (and HashSets silently ignore when a duplicate element is an argument of the add method), which means the user didn't win the game.

So yeah i mean i guess everyone has their own style. Different people like using different classes that do virtually the same thing. Its just a matter of preference.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-04-2008, 10:41 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks all of you. It was a simple thing that I was not thinking of:

Code:
int size = 10; String[] array = new String[size];
But I learnt that if size is not defined, we can use ArrayList. And also generics can be used.

Again, thanks guys.
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
Array Help bluegreen7hi New To Java 2 03-28-2008 03:25 AM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 03:34 PM
2D array bluekswing New To Java 2 01-15-2008 06:57 PM
Method parameters bounded by a Class JavaForums Java Blogs 0 11-21-2007 02:31 PM
Help with Array susan New To Java 1 08-07-2007 05:32 AM


All times are GMT +3. The time now is 02:32 PM.


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