LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2007, 12:14 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
Java Question


Hi there
I would be really obliged to anyone that could help me.
I am actually interested in constructing a dynamic object.
To be more clear, if there is an object which has an array of integers as private instance and this array at a given time is full and we want to add one more variable (integer) then we have a "problem".
On the other hand if we have an object with a list as private instance then we can add to that list as much as we want.
As I have seen there are a number of types of Lists in Java(LinkedList, doubleLinkedList, etc). Although that I am really familiar with data-structures in C, it seems that i am really struggling with Java.
I would be happy if anyone could give me an example of constructing such an object.

Thanks
 
Old 10-22-2007, 12:41 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
If you want simply add one more integer to an "array", you may want to use the java.util.ArrayList<K> class. It's just a class that provides a nice, manageable collection.

Code:
import java.util.ArrayList;
...
...
ArrayList<Integer> ali = new ArrayList<Integer>();
...
I haven't written Java in quite some time, so I can't begin to help you with any of the public instance methods for that class. I also think the syntax for the constructor may have changed slightly...I can't remember. If you're using Eclipse or NetBeans, it'll guide you in the right direction for the constructor syntax.
 
Old 10-22-2007, 02:35 PM   #3
lamar_air
Member
 
Registered: Oct 2004
Posts: 37

Rep: Reputation: 15
If you're looking for an array that can automatically resize then vectors are easy to use for this. They just aren't recommended when performance is a high priority. Just search for "java vector".
 
Old 10-22-2007, 05:45 PM   #4
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Hi and thanks for your replies
I would like to ask something regarding the piece of code that you provided.
Is the following possible?
Quote:
ArrayList<MyObject> ali = new ArrayList<MyObject>();
MyObject of course is a class that we will have created.
 
Old 10-22-2007, 09:29 PM   #5
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Absolutely - as long as MyObject implements Object (which EVERYTHING in Java does by default) then you're good!
 
Old 10-22-2007, 09:33 PM   #6
turbo_spool
LQ Newbie
 
Registered: Oct 2007
Location: Rome, NY
Distribution: Slackware, openSUSE, Fedora Core
Posts: 18

Rep: Reputation: 0
Quote:
Originally Posted by indienick View Post
Absolutely - as long as MyObject implements Object (which EVERYTHING in Java does by default) then you're good!
Thaaats correct!

To add to that: just remember that with Java, the Javadocs are your friend. Big time. When I need to use a class that I am unfamiliar with, my first step is to google.

GOOGLE: java SomeClass class

That usually gets me started.
 
Old 10-23-2007, 08:43 AM   #7
lamar_air
Member
 
Registered: Oct 2004
Posts: 37

Rep: Reputation: 15
By using:
Code:
ArrayList<MyObject> ali = new ArrayList<MyObject>();
instead of:
Code:
ArrayList ali = new ArrayList();
You're using "Generics" which just means that you're only allowing this ArrayList called ali to hold objects of type "MyObject". You can simplify it by not specifying that it must hold MyObjects but it's better practice to specify it like you did. It's better practice because then whoever uses ali knows that it only contains MyObjects..
 
Old 10-23-2007, 08:46 AM   #8
lamar_air
Member
 
Registered: Oct 2004
Posts: 37

Rep: Reputation: 15
More info about generics here: http://java.sun.com/j2se/1.5.0/docs/.../generics.html
 
  


Reply

Tags
java



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question in java... scottcky1985 Programming 2 08-20-2007 02:17 AM
Java question thefalling Programming 2 11-22-2006 02:01 PM
Java question Jongi SUSE / openSUSE 2 09-19-2005 01:07 PM
Java Question k1ll3r_x Programming 2 08-20-2005 08:10 PM
Java Question k1ll3r_x Programming 3 11-13-2004 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration