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.
Ok I have an array full of 10 objects. This array is called carArray..
Now I wish to add a new object to the array but am having trouble.
I started off thinking that I should create a tempoary array one index bigger then the current array, copy the information from the first array to the second array and then create the original array again but one bigger then copy the information from the temp array to the original... however when I try to create the original array again it says that it already exsists and wont compile..
here is my "copying array" code...
Car [] tempArray;
tempArray = new Car [carArray.length+1];
System.out.print(" Length = " + tempArray.length);
for (int x = 0; x <carArray.length; x++)
{
tempArray[x] = carArray[x];
}
System.out.print(tempArray[9].getMillage()); // to test objects been copied correctly...
Now this works fine I'm just not sure what to do now... I thought about maybe recreating the first array like this...
Car [] carArray;
carArray = new Car [tempArray.length];
but it already exsists and so wont compile,how do I increase the lenghth of the original array???
Well the size of the arrays in Java is static and has to be determined at the compile time.
You cant change at the run time . You can use ArrayList or Vectors for dynamically resizing your object container.
What you are doing , it will make a new array and you will lose the original array. So i dont think it is the best thing to do.
Nah it works cool, I move all the data (objects) to a temp array first and then resize the original array. Then after that just copy all the data back.
class NameHandler
{
//let us start by creating our array
String [] nameArray = new String [3];
public static void main(String [] args)
{
//lets add some names to our array
nameArray [0] = "Dave";
nameArray [1] = "Steve";
nameArray [2] = "Mary";
//it should be full now, so we need to extend it
extendArray();
//now we can add some more names
nameArray [3] = "Jo";
nameArray [4] = "Richard";
}
private static void extendArray()
{
//create a new array 3 larger than the original
String tempArray = new String [nameArray.length + 3];
//copy all of the data to the new array
for(int i=0; i<nameArray.length; i++)
tempArray [i] = nameArray [i];
//since the variables are just pointers we just need to make the
//nameArray point to our new array
nameArray = tempArray;
}
}
Because I was a stupid noob who was doing his first term of Java programming and didn't know what I was doing!
Hey ... "not (yet) knowing what you're doing" is just par-for-the-course. No reason to speak ill of yourself.
After all, this is Java we are talking about here!
But, anyhow: IMHO, most of the time you should be using container classes, not fixed-size things such as "true arrays" (which Java does support, while PHP, say, does not). The implementation of these classes will handle all of the usual operations: insert, append, delete, push, shift, count, empty, retrieve. And they will do so very, very efficiently. (Well, at least as efficiently as Java can manage to do anything at all ...) *koff, koff ...*
And, really, the same is now true of many other languages: C#, Delphi, C++, etc. If the language provides containers, then you should use them most of the time. It's also important to understand a little bit about the underlying implementation of each class, because, "every data structure is some kind of compromise."
Last edited by sundialsvcs; 08-16-2016 at 08:13 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.