Hi all

Am trying to Truncate the end of a linear list in java. so that:

// reduce the size of the list to s by removing all elements
// with indices greater or equal to s; return the number of removed
// elements;
// for example, if the list is [d, a, b, c, g, a], then truncateEnd(4)
// should change it to [d, a, b, c] and return 2;
// don’t do anything (and return 0), if the size of the list
// isn’t greater then s

public int truncateEnd(int s) {

// there is a method called remove() which takes as parameters specific index of the list and removes 
// that! there is also a method called indexOf() that returns the specific index number of a given element

}

I have tried a few things but got no where! I tried a for loop but then realised that every time it goes the round the index of the specific elements are moving down one!

Any help would be much appreciated!!

ps the listed is manually implemented so its not an Array list or anything like that!

honestly, I have no idea what you mean ...
why is it not an array? why doesn't it work in a for loop? since, how I see it, a for loop will be needed in (at least) one step of your work.

Well, lets see what you've tried. But really, all you need to do loop from the end of list forwards until you reach the provided index.

Sorry if I didn't make clear what I needed. The code I tried is as follows:

public int truncateEnd(int s) {
int counter = 0;
     for (int i = s-1; i<size-1;i++)
      {
          counter++;
          remove(i);
      }


}

Code:

// reduce the size of the list to s by removing all elements
// with indices greater or equal to s; return the number of removed
// elements;
// for example, if the list is [d, a, b, c, g, a], then truncateEnd(4)
// should change it to [d, a, b, c] and return 2;
// don’t do anything (and return 0), if the size of the list
// isn’t greater then s

public int truncateEnd(int s) {

// there is a method called remove() which takes as parameters specific index of the list and removes 
// that! there is also a method called indexOf() that returns the specific index number of a given element

}

I have tried a few things but got no where! I tried a for loop but then realised that every time it goes the round the index of the specific elements are moving down one!

Any help would be much appreciated!!

ps the listed is manually implemented so its not an Array list or anything like that!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

First why "s-1"? Second, what is wrong with what you've tried? IOW what exceptions are you getting or what is happening that should not be happening or what is not happening that should be happening?

orry that last post was incomplete! lets try again!

int counter = 0;

if (s < size) {
for (int i = s-1; i <= size; i++) {
counter++;
remove(i);
}
} else
{
return 0;
}

return counter;
the list before calling this method was: [9, 6, 1, 5, 8, 9, 9, 11]
and after it was: [9, 6, 1, 8, 9]

if we were to say truncateEnd(4)
it should be: [9, 6 , 1, 5]
and should return 4 (as it has remoed 4 elements)

its s-1 cause the list is from 0 t0 8 lets say, so s-1 would delete the index s-1 which would be 3 rather than 4. I am not getting an exception juts not the right result!

The description says, however "removing indices greater or equal to s".

Also, start from the end of the list and move forwards. When you start from the "front" (or from the index to remove) the following happens.

List -> 1 2 3 4 5 6 7 8 9 0
Remove index 4 (the 5) and increment the index to 5
List -> 1 2 3 4 6 7 8 9 0
Remove index 5 (now the 7) and increment the inde to 6
List -> 1 2 3 4 6 8 9 0
Remove index 6 (now the 9) and increment the index to 7
List -> 1 2 3 4 6 8 0
End because the index is beyond the end.

cannot believe my problem was starting from beginning, and am sure i tried to count down too but didn't work!!! wither way thanks for your help guys

This article has been dead for over six months. Start a new discussion instead.