?
Solved

Deleting an array item ???

Posted on 2005-03-13
7
Medium Priority
?
258 Views
Last Modified: 2010-03-31
Hi everybody, I'm trying to delete an array item setting the item to null, but each time I read the array after that I get an error... Is there a way to do this ? I also tried copying every items except the one I didnt want to another array but it didnt worked =(

Here's my function for consultation:

public static void DeleteActors(Movie[] arrMovies, Actor[] arrActors, String number)
      {
            Actor[] arrActorsTmp= new Actor[50];
            int nbActors = 0;
            
            for (int k = 0; k < arrActors.length - 1; k++)
            {
                  if (number.equals(arrActors[k].getNumber()))
                  {
                        k++;
                  }
                  else
                  {
                        arrActorsTmp[k] = arrActors[k];
                        nbActors ++;
                  }
            }
            
                // **** When I try to print, I get an error where I didn't copy the item to delete *****
            for (int i = 0; i < nbActors - 1; i++)
            {
                  System.out.println("Actor: " + arrActorsTmp[i].getName());
            }
      }


Thanks for the help,
Frank
0
Comment
Question by:The_Kingpin08
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13532654
what exactly is it you are trying to do?
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 13532681
try this in ur printing area

if(arrActorsTmp[i].getName() != null)
{
System.out.println("Actor: " + arrActorsTmp[i].getName());
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 2000 total points
ID: 13532687
Couple of errors. You've used the same variable 'k' for indexing both arrays. You should use a separate one for indexing the second array. Also - while looping, you are looping from 0 to < length - 1, which is wrong. You should loop from 0 to < length, or from 0 to <= length - 1.

Try this:

public static void DeleteActors(Movie[] arrMovies, Actor[] arrActors, String number)
     {
          Actor[] arrActorsTmp= new Actor[accActors.length];
          int nbActors = 0;
         
          for (int k = 0, j = 0 ; k < arrActors.length ; k++)
          {
               if ( ! number.equals(arrActors[k].getNumber()))
               {
                    arrActorsTmp[j++] = arrActors[k];
                    nbActors ++;
               }
          }
         
          for (int i = 0; i < nbActors ; i++)
          {
               System.out.println("Actor: " + arrActorsTmp[i].getName());
          }
     }

By the way, what is the use of the Movies array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:clim1219
ID: 13532704
Why not use a java.util.Map instead?
0
 

Author Comment

by:The_Kingpin08
ID: 13532716
I'm trying to delete an item from an array. The only way I thought it was possible was to copy all the information (except the one you want to delete) to another array...

Is there a better way ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13532717
And I still don't see an array item getting deleted (as per what the question-title says) ;)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13532721
>> The only way I thought it was possible was to copy all the information (except the one you want to delete) to another
>> array...

If you do that, you need to return the new array back.

You can also use Collections for your purpose. An ArrayList or a HashMap for example (depending upon what you need) - in that case, adding and deleting will be more flexible.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question