adding and deleting from an array

adding or deleting one name to/from a list of names

Page 1 of 1

3 Replies - 2830 Views - Last Post: 24 August 2007 - 03:39 PM Rate Topic: -----

#1 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

adding and deleting from an array

Post icon  Posted 24 August 2007 - 09:13 AM

Does anyone have suggestions or know of someplace I can look for examples of this kind of coding? This looks like it should be a simple procedure - but I have been known to turn a "simple" procedures into something of great proportions . . . LOL I am posting the code for the "add to" array so you will understand what I am talking about. My instructor gave me documentation at Sun to look at and I am having trouble understanding it.

Here are the instructions given by my instructor about how to do this participation question (it is not an assignment, but will give me participation points and knowing how to add/delete from an array will help with the assignment):

Question: After reading the Background and Example sections below, explain how you would resize your array of sub-class objects to add a new object to your array. (By "explain" I mean write some Java code showing how to do it. You will obviously have to generalize from the String class in the example and apply this to your class.)

Background

Normally, arrays are fixed in length for the entire duration of your program and other objects (such as the List and ArrayList) are used when you need to easily add elements. However, you can add to an array and I show you how using the method System.arraycopy. You will need to import java.util.Arrays wherever you use this method.

This method has the following syntax:

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

(The arrays are the Object src and Object dest parts.)

For reference, please see its documentation http://java.sun.com/...ang/System.html
(look for arraycopy).


Example

Suppose we start with an array of names:

String name[] = {"Tom", "Jerry", "Susan" };

Let's say we wish to add the new name "Mandy" to the end of this array. In that case, we can write the following code:

//Make a temporary array variable for an array one size larger than name
String temp[] = new String[ name.length + 1 ];

//Copy the contents of the name array to the temp array
//That is, copy name[0]...name[name.length-1] to temp[0]...temp[name.length-1]
System.arraycopy( name, 0, temp, 0, name.length );

//Now add the new name at the end of the larger temporary array
temp[name.length] = "Mandy";

//Now "seal the deal" by copying the array variable temp to the array variable name
name = temp;

This is the output one obtains when compiling and running the example program below:

***Before Adding**
Tom
Jerry
Susan

***After Adding**
Tom
Jerry
Susan
Mandy




//In TestAdd.java

import java.util.Arrays; //For arraycopy



public class TestAdd
{
   public static void main(String args[])
   {

	   String name[] = {"Tom", "Jerry", "Susan" };



	   //Print out before adding
	   System.out.println("***Before Adding***");
	   for( String s: name )
	   {
			System.out.println( s );



	   }//End for


	   //Make a temporary array variable for an array one size larger than name
	   String temp[] = new String[ name.length + 1 ];


	   //Copy the contents of the name array to the temp array
	   //That is, copy name[0]...name[name.length-1] to temp[0]...temp[name.length-1]
	   System.arraycopy( name, 0, temp, 0, name.length );


	   //Now add the new name at the end of the larger temporary array
	   temp[name.length] = "Mandy";



	   //Now "seal the deal" by copying the array variable temp to the array variable name
	   name = temp;



	   //Print out after adding
	   System.out.println("\n***After Adding***");
	   for( String s: name )
	   {
		   System.out.println( s );

	   }//End for

   }//End main



}//End TestAdd



Most likely I should not have even posted this - it looks so easy . . . .
and the instructions from my instructor most likely explain it all if I can figure out what src, and dest are, I should be able to do this.

My real assignment is not easy tho . . . I want to do this to learn from it, but I don't want to use up all my time on it . . . . it is worth about 3 or 4 participation points; so what I learn will be more valuable to me.

Thank you!
:)

-bigbluesky

Is This A Good Question/Topic? 0
  • +

Replies To: adding and deleting from an array

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: adding and deleting from an array

Posted 24 August 2007 - 09:53 AM

So, what problems are you having? It looks like you've pretty much got a handle on the idea of what you're doing. The only thing I would caution you on is to watch your array boundaries. If you have an array called "a", of length 4, it has indices 0, 1, 2, and 3. But if you call a.length, it will return 4. So, do you see a problem trying to use a.length to reference that last array index? Look through your code to see where you have used this and see if you are using it correctly.
Was This Post Helpful? 0
  • +
  • -

#3 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: adding and deleting from an array

Posted 24 August 2007 - 10:06 AM

View Postalcdotcom, on 24 Aug, 2007 - 09:53 AM, said:

So, what problems are you having? It looks like you've pretty much got a handle on the idea of what you're doing. The only thing I would caution you on is to watch your array boundaries. If you have an array called "a", of length 4, it has indices 0, 1, 2, and 3. But if you call a.length, it will return 4. So, do you see a problem trying to use a.length to reference that last array index? Look through your code to see where you have used this and see if you are using it correctly.



I wish I could write code that good . . . . this code is from my instructor, I am supposed to modify it - by adding to the array. I did it a really "cheap" way - I know it is wrong - it was too easy. So now I will try to do it the "right" way. I will try things and experiment some more.

Thanks for the advice on the length, I had not thought of that yet.

:)

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: adding and deleting from an array

Posted 24 August 2007 - 03:39 PM

View Postalcdotcom, on 24 Aug, 2007 - 09:53 AM, said:

So, what problems are you having? It looks like you've pretty much got a handle on the idea of what you're doing. The only thing I would caution you on is to watch your array boundaries. If you have an array called "a", of length 4, it has indices 0, 1, 2, and 3. But if you call a.length, it will return 4. So, do you see a problem trying to use a.length to reference that last array index? Look through your code to see where you have used this and see if you are using it correctly.

Ooh - my bad. I mis-read the code. He's not misusing a.length because he's using the length of another array. That's what happens when you get busy and in a hurry...you miss simple stuff like that. :)


View Postbigbluesky, on 24 Aug, 2007 - 10:06 AM, said:

View Postalcdotcom, on 24 Aug, 2007 - 09:53 AM, said:

So, what problems are you having? It looks like you've pretty much got a handle on the idea of what you're doing. The only thing I would caution you on is to watch your array boundaries. If you have an array called "a", of length 4, it has indices 0, 1, 2, and 3. But if you call a.length, it will return 4. So, do you see a problem trying to use a.length to reference that last array index? Look through your code to see where you have used this and see if you are using it correctly.



I wish I could write code that good . . . . this code is from my instructor, I am supposed to modify it - by adding to the array. I did it a really "cheap" way - I know it is wrong - it was too easy. So now I will try to do it the "right" way. I will try things and experiment some more.

Thanks for the advice on the length, I had not thought of that yet.

:)

-bigbluesky


Post your code and I (or someone else) might be able to help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1