开发者

List set index c#

I am using a List construct to handle the sequence in which images are drawn in "OnPaint". Now if my images are re-ordered (e.g. "brought to front" or "..to back"), I'd need to reposition them in my List. I have trouble doing so, because List doesn't support a method similar to setIndex().

So what I am trying to do is basically:

    private List<BitmapWithProps> activeImages = new List<BitmapWithProps>();

    public void addActiveImage(BitmapWithProps image)
    {
        activeImages.Add(image);
    }

    public BitmapWithProps getActiveImage(int index)
    {
        return activeImages[index];
    }

    public void removeActiveImage(int index)
    {
        activeImages.RemoveAt(index);
    }

    public void removeActiveImage(BitmapWithProps item)
    {
        activeImages.Remove(item);
    }

    public void swapActiveImageIndex(int sourceIndex, int destIndex)
    {
        // what would the code look like in here if I were to swap
        // the 2nd item (1) with the 4th one (3) in a 5-item-List (0 - 4)
    }

I want to be able to swap an index.. sort of. I could "insert" a new item into the List at the index it should go to a开发者_如何转开发nd assign the value and then delete the other "source". However it doesn't seem elegant in any way.

I'd be glad for any hints and please excuse me if I overlooked a thread - I did search though, before asking.

dS.


What's inelegant about doing the steps you want to do? If you want to change something's position within a List (which is a vector style collection) then what you want to do is insert it at the new position and remove it from the old. Precisely what you are complaining about having to do.

If it really upsets you then write an extension method:

public static void MoveIndex<T>(this List<T> list, int srcIdx, int destIdx)
{
  if(srcIdx != destIdx)
  {
    list.Insert(destIdx, list[srcIdx]);
    list.RemoveAt(destIdx < srcIdx ? srcIdx + 1 : srcIdx);
  }
}

Edit: Oh, you just want to swap? Simpler (and more efficient) still:

public static void SwapItems<T>(this List<T> list, int idxX, int idxY)
{
  if(idxX != idxY)
  {
    T tmp = list[idxX];
    list[idxX] = list[idxY];
    list[idxY] = tmp;
  }
}


Well, just performing a swap is easy:

BitmapWithProps source = activeImages[sourceIndex];
BitmapWithProps dest = activeImages[destIndex];
activeImages[destIndex] = source;
activeImages[sourceIndex] = dest;

But if you need to move one item and leave all other items in the same order with relation to each other, you should call RemoveAt followed by Insert. It won't be terribly efficient, but unless you've got a large list or you're doing this very often, it's unlikely to really cause you any problems.


public void swapActiveImageIndex(int sourceIndex, int destIndex)
{
    var source = activeImages.Item[sourceIndex];
    var dest = activeImages.Item[destIndex];

    activeImages.Item[sourceIndex] = dest;
    activeImages.Item[destIndex] = source;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜