ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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".
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.
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.