?
Solved

Deleting an array item ???

Posted on 2005-03-13
7
Medium Priority
?
263 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

580 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