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-07-2007, 04:32 PM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
object instantiation and arrayList
Hi. I am reasonably new to Java but know the basics. I am just wandering if i could get some small help...

I am creating an Enrolment System for a University (its my assignment) and I have a class called Unit, and a class called Enrolment. In the Enrolment class, I am writing a method for the user to create a new Unit, but I have the following problem, which might be hard to explain.

Here is some of the code from the top...

Code:
import java.util.ArrayList; import java.util.Scanner; public class Enrolment { Scanner scanner = new Scanner(System.in); private ArrayList<Unit> units; public Enrolment() { units = new ArrayList<Unit>(); }
and here is the code from where I actually create the unit once I have the information from the user...

Code:
Unit 1005 = new Unit(name,code,sem1,sem2); units.add(1005);
My question is... where it says '1005' (the name of the instance of Unit that I create)... is their a way so that it will change with every new unit? Perhaps so it will be the name of a variable, such as 'code'?? I tryed something like...

Code:
Unit code = new Unit(....)
but obviously that just created it called 'code'.

Just wandering what ways are their of making this change everytime a new instance is created.

Thanks for your help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 01:39 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
First correction in the code I like to make is we usually wont create objects of type ArrayList but only List
Second I dont think 1005 is a valid name ....
Ooooooooh enough of finding the mistakes Sorry dude

About your question what I understood you need is the little bit complicated In fact I dont know the way to do that .. ie
"the variable name will be dynamic "

Actually do you really need something like that , From my three years of programming I was happy with the 'static' variables

Cant you tell me a situation you will need this
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 02:16 AM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
Quote:
Originally Posted by goldhouse View Post
First correction in the code I like to make is we usually wont create objects of type ArrayList but only List
Second I dont think 1005 is a valid name ....
Ooooooooh enough of finding the mistakes Sorry dude

About your question what I understood you need is the little bit complicated In fact I dont know the way to do that .. ie
"the variable name will be dynamic "

Actually do you really need something like that , From my three years of programming I was happy with the 'static' variables

Cant you tell me a situation you will need this
Thanks for the reply mate. Well if I keep the name of the instance static (e.g. Unit101) and have that hardcoded into the program, wont everytime a new unit is created it will just point to the previous unit that was created (as they would have the same instance named of Unit101)? I don't really care at all what the name of instance is because it then goes into the ArrayList once created anyway from where I can just access it by index........

OO yeh the name 1005 is just for illustration purpose... and also what do you mean about calling the object List? I thought that was the correct way to use the ArrayList...

cheers

Last edited by lockmac : 08-08-2007 at 02:26 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-09-2007, 12:39 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Ok ArrayList in Java is an implementation of the interface List, Good programmers using the objects only for instantiation and use the interfaces for all the other purpose so you need not change the code at many places once you want to change the implentation.
That is why I told you to use List instead of ArrayList
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-09-2007, 03:58 AM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
Ok cheers for that I see where your coming from and have now fixed the problem.

Have a good one guys

Last edited by lockmac : 08-09-2007 at 10:09 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-09-2007, 07:25 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
You are welcome
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
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
Object ArrayList - increment solution needed badly!! rugbyGeek New To Java 4 03-08-2008 01:47 AM
instantiation Camden New To Java 1 03-02-2008 05:44 PM
how to return an object from an arraylist elizabeth New To Java 1 07-30-2007 07:57 PM


All times are GMT +3. The time now is 05:27 PM.


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