LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-17-2004, 02:05 AM   #1
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 18

Rep: Reputation: 0
Java Array's.... Making An array bigger???


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???

thanks alot...
 
Old 05-17-2004, 03:52 AM   #2
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 18

Original Poster
Rep: Reputation: 0
Never mind... i was being very stupid.. I don;t need to put

Car [] carArray;

in again when editing the length of the first array//

this is my first time at using arrays so I don't think i'll be making that mistake again... oh well... onto writing to a file now... looks scary..
 
Old 05-17-2004, 07:20 AM   #3
villie
Member
 
Registered: Apr 2004
Location: Pakistan
Distribution: RedHat
Posts: 46

Rep: Reputation: 15
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.

Regards
Villie
 
Old 05-17-2004, 07:28 AM   #4
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 18

Original Poster
Rep: Reputation: 0
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.
 
Old 05-17-2004, 08:51 AM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
That will work, but it's very inefficient. You should use the Vector class.

Alex
 
Old 05-17-2004, 03:14 PM   #6
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
First off, ArrayList is the way to go

secondly, this will make your array bigger
Code:
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;
   }
}
 
Old 05-17-2004, 05:38 PM   #7
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Or a slight variation



System.arraycopy(originalArray,0,tempArray,0,originalArray.length);

originalArray=tempArray;


why not use a dynamic class though
 
Old 08-15-2016, 05:18 PM   #8
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 18

Original Poster
Rep: Reputation: 0
Cool

Because I was a stupid noob who was doing his first term of Java programming and didn't know what I was doing!

EDIT - Oh and apologies for the late reply.

Last edited by Tru_Messiah; 08-15-2016 at 05:35 PM.
 
Old 08-16-2016, 08:11 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
Quote:
Originally Posted by Tru_Messiah View Post
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.
 
Old 08-16-2016, 08:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I have heard of replying late ... but 12 years is a bit of stretch to call late
 
Old 08-17-2016, 01:05 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
Yeah, I knew that it was an old thread, but the same issues keep coming up again and again.
 
Old 08-17-2016, 01:45 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@sundialsvcs - I found your reply useful. Was more commenting on the OP's late reply
 
  


Reply



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
Making a partition bigger Tsukasa7 Linux - Newbie 8 10-24-2005 12:41 PM
making linux partition bigger? M O L8ingN2dust Linux - Newbie 1 03-23-2005 03:47 PM
Making the /boot partition bigger r3dhatter Linux - Newbie 28 07-12-2004 08:28 PM
Making a partition bigger rverlander Linux - General 8 07-19-2002 08:49 PM
making patitions bigger than one harddrive saavik Linux - Software 3 11-10-2001 07:59 AM

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

All times are GMT -5. The time now is 11:17 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