0

Lets say I have this loop and I put the data in an ArrayList:

int Num1 = Int32.Parse(textBox1.Text);
int Num2 = Int32.Parse(textBox2.Text);
ArrayList ItemList = new ArrayList();
while (Num1 <= Num2)
{
    ItemList.Add(Num1);
    Num1++;
}

And I have another loop to read my Arraylist:

foreach (int item in ItemList)
{
    listBox1.Items.Add("Number " + item.ToString() + ",");
}

Which gives this result:

Number 1,
Number 2,
Number 3,
Number 4,

I need to remove the last comma in the last item and get this result:

Number 1,
Number 2,
Number 3,
Number 4

I've tried this:

foreach (int item in ItemList)
{
    listBox1.Items.Add("Number " + item.ToString().Trim(',') + ",");
}

But it doesn't work. Can someone please tell me what I did wrong, and how I can fix it?

1
  • That's a bit confusing. This is not really about deleting an element from an array, right? Commented Apr 23, 2015 at 19:20

4 Answers 4

6

See if this works for your purposes:

var result = string.Join("," + Environment.NewLine, itemList.ToArray());

Forgot the "Number" part:

var result = string.Join(", " + Environment.NewLine, itemList.ToArray().Select(x => "Number " + x));
Sign up to request clarification or add additional context in comments.

9 Comments

I was about proposing the same. Neat, clean, simple.
Of course, instead of adding integers 1, 2, 3... you would need to a "Number 1", "Number 2", "Number 3" .... to your ArrayList instead.
That will use the String.Join(string, params object[]) overload, so the result is "System.Collections.ArrayList".
Yeah either call itemList.ToArray() or just use a List<string> instead of the ArrayList
@JoshL. Thanks a Lot for help and sorry if i waste your time
|
3
listBox1.Items.Add("Number " + item.ToString() + 
ItemList.IndexOf(item) == ItemList.Count - 1 ? string.Empty : ",");

2 Comments

What's the complexity of IndexOf() again? Was it O(N) maybe? And what if we are doing this in a loop?
thanks but it dosn't work i have a red line this part ItemList.Count()
0

you should not modify the item of your collection inside a foreach.

May be you should try to modify your collection ItemList before the foreach that shows your result .

You can make somthing like this (before the foreach that shows your results):

ItemList[ItemList.Length-1] = ItemList[ItemList.Length-1].SubString(0,ItemList.Length -2);

Comments

0

How about this:

foreach (int item in ItemList)
{
    if (listBoxOne.Items == null)
    {
        listBox1.Items.Add("Number " + item.ToString());
    }
    else
    {
        listBox1.Items.Add(",\n" + "Number " + item.ToString());
    }

    //Or more simply below
    listBoxOne.Items.Add = listBoxOne.Items == null ? ("Number " + item.ToString()) : (",\n" + "Number " + item.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.