Question:
Array Question?
Carebear
2007-11-15 21:04:42 UTC
I am working on a project for my class and our goal is to create an array and be able to add and remove cat objects to it. I have successfully created the array, and I am capable of putting cats into the array. The problem occurs when I got to remove the cats. The first cat I ask it to remove, is removed successfully. But it removes the entire slot, not just the cat.

So I have
1. Fluffy
2. Puss
3. Ralph

I remove Puss and spits out
1. Fluffy
3.Ralph

Ok, so it keeps running fine from there until I got remove another cat. When I do that, it starts cycling through the list and goes:
1. Fluffy It looks at this line and says "No cats by that name" (because I'm not trying to remove fluffy)
and then it stops. The code won't continue running. So I assume the problem lies with the fact that the 2. line went away and it can't run through the for loop because of it.

My question then becomes, how do I move everything up a space once something has been removed?
Three answers:
DogmaBites
2007-11-15 21:51:57 UTC
I'm glad you made an effort to do this on your own before posting. Many people just post the question and want someone else to do their work for them.



You are trying to maintain a contiguous list. It's not hard to move everything down one space when you delete.



Instead of setting

catKennel[i] = null;



execute this loop:

for (int j = i; j < myCatNumber - 1; ++j)

{

catKennel[j] = catKennel[j + 1];

}



What this loop does is, starting with the one to delete, move the cat one spot ahead to this spot.



Then set the last item to null

catKennel[myCatNumber - 1]

I'm assuming it's OK to set an array element to null in Java, I'm not a Java person myself.



Also, move the else block outside the loop. You shouldn't say no cats by that name unless you have inspected the entire kennel.



// Move this after the loop

System.out.println( "Sorry, we don't have any cats by that name." );
xytose
2007-11-16 05:38:27 UTC
Hello,



The concept you have is pretty simple, but arrays are not fit to do this job. The problem with arrays is that they have constant index limit, especially if you're talking about c-based languages like C/C++, C#, Java, ActionScript, ect... LinkLists or ArrayLists were made to do this job because they have a limitless bounds, so you can actually delete, add, even insert objects into certain positions of the list endlessly.



In any case, if you still persist on using arrays I suggest that you create an array that very large like 255 and make sure your inputs don't touch this limit. Next, create a used index bool array that corresponds with the indexices of the string array. Create a function that allows you to remove and add to the array, everytime you add to the array, set the index value of your bool array to true for that position, then change the value of your array at that position. Below is an example of how i would do it. (code not tested)





// note: im assuming you're coding in C++

class Cats

{



public bool isEmptyPos[255];

public string NameOfCats[255];



public Cats()

{



for(int i=0; i < 255; i++)

isEmptyPos[i] = true;



}





public int FindLowestEmptyIndex()

{



for(int i=0; i < 255; i++)

{



if( isEmptyPos[i] ) return i;



}



return -1;

}







public bool addCatName(string CatName)

{



if(name.length > 0)

{

if(FindLowestEmptyIndex() > -1)

{

int localIdx=FindLowestEmptyIndex();

NameOfCats[localIdx] = CatName;

isEmptyPos[localIdx] = false;



return true;

}



}

return false;

}



}





public void RemoveCatByName(string CatName)

{



for(int i=0; i<255; i++)

{



if(!isEmptyPos[i])

{



if(NameOfCats[i] == CatName)

{

isEmptyPos = true;

//note: clearing the actual string array

//at this point is pointless because isEmptyPos

//is what we use to determine if an array element

//is empty or not

}

}

}





public string printAllCatNames()

{



string catNames = "The cats include:\n";



for(int i=0; i<255; i++)

{

if(isEmptyPos[i])

catNames+=(NameOfCats[i]+"\n");

}



if(catNames=="The cats include:\n")

return "No cats have been added";



return catNames;

}







}





I hope that helps. Again, this code is not tested, but you should get the general idea.
Expolorer
2007-11-16 09:24:48 UTC
this approach dont need to move one space after removing..

try this..



public void addCat( Cat icat )

{

int i;



// look for the room for new Cat

for (i=0; i < catKennel.length;i++ ){

/* make sure that you initialize the catKennel[] to " " */

if (catKennel[i] == " ") {

catKennel[i] = icat;

myCatNumber++;

return;

}

}

if(i==catKennel.length){

System.out.println( "Sorry, no more room for cats," + icat.getName() + " not added." );

}

}



public void removeCat( String iname )

{



for( int i = 0; i < catKennel.length; i++ ){

if( iname == catKennel[i].getName() ){

System.out.println( catKennel[i].getName() + " has been removed." );

catKennel[i] = " "; // clean it so that you can still use it.

myCatNumber--;

return;

}

else{

System.out.println( "Sorry, we don't have any cats by that name." );

}

}



hope this helps..:D

cheers!!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...