Hi, i have some problems with arraylist:

ArrayList arr=new ArrayList();
string a="1 2";  // how to split this and store to arraylist?

How to split the string "1 2" and store into arrayList arr?

Recommended Answers

All 9 Replies

This question has been asked by a lot of other people in Malayasia. Are you all in the same class? Why don't you post the real problem you are trying to solve so we can suggest a better way to solve it.

4

This question has been asked by a lot of other people in Malayasia. Are you all in the same class? Why don't you post the real problem you are trying to solve so we can suggest a better way to solve it.

4

ok, but it is a bit complex, i try to simplify my problems as following:
I have a listbox which contains items as below:

1 2
1 2 3
1 3 4
2 4 5
//something like that

Then, i have a sortlist which the keys and values as below:
1 20
2 30
3 10
4 15
5 45


1) Firstly, for each items ( for example,"1 2") in listbox,i wants to split "1 2" and store to arraylist, after that compare with sortlist.keys.

2) Next,
if "1" and "2" is match with keys(1 and 2) in the sortlist, it will take the value (20 and 30).The values will compare with a fix value(20) ,if equal or bigger than the fix value, then both the "1 2" will store into a new arraylist.

3) if one of the value of "1 2" is lower than the fix value, then it will break and go to next item in listbox(1 2 3).

That is my problem, how should i do?

I had tried my way, but no idea how to proceed, someone please help me..

ArrayList ar1=new ArrayList();
                ArrayList ar2=new ArrayList();
                double value1;
                foreach (string item in listbox.Items)
                {
                    //How to split and store to ar1?

                    foreach(string item1 in ar1) // Inside this loop, how to do the logic? i no idea how to compare "1 2" 1 by 1
                    {
                        if ((sortList3.ContainsKey(item1)))
                        {
                            sortList3.TryGetValue(item1, out value1);
                            if (value1 >= 20)
                            {
                                ar2.Add(item);
                            }
                            else
                                break;
                        }      

                    }

                }

hi..

try something like this:

ArrayList newArry = new ArrayList();

                SortedList SortList =new SortedList();
                SortList.Add("1", "20");
                SortList.Add("2", "30");
                SortList.Add("3", "10");
                SortList.Add("4", "15");
                SortList.Add("5", "45");


                foreach (string item in listBox1.Items)
                {
                    string[] itemcol = item.Split(' ');   // splitting items by space

                    foreach (string str in itemcol)
                    {
                        if (SortList.ContainsKey(str))
                        {
                            string value = SortList[str].ToString();

                            if (int.Parse(value) >= 20)
                            {
                                newArry.Add(item);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

hi..

try something like this:

ArrayList newArry = new ArrayList();

                SortedList SortList =new SortedList();
                SortList.Add("1", "20");
                SortList.Add("2", "30");
                SortList.Add("3", "10");
                SortList.Add("4", "15");
                SortList.Add("5", "45");


                foreach (string item in listBox1.Items)
                {
                    string[] itemcol = item.Split(' ');   // splitting items by space

                    foreach (string str in itemcol)
                    {
                        if (SortList.ContainsKey(str))
                        {
                            string value = SortList[str].ToString();

                            if (int.Parse(value) >= 20)
                            {
                                newArry.Add(item);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

Here will have a problem, let say "1 2", if "1" is higher than 20, but "2" is lower than 20 , it also will add "1 2" to the newArry, this is not expected by the result.

Besides that, if both the "1" and "2" also higher than 20, then it will add 2 times “1 2” to the newArry.. This is also a wrong ..

for you first query : (Here will have a problem, let say "1 2", if "1" is higher than 20, but "2" is lower than 20 , it also will add "1 2" to the newArry, this is not expected by the result.)

change the code to this:

ArrayList newArry = new ArrayList();

                SortedList SortList =new SortedList();
                SortList.Add("1", "20");
                SortList.Add("2", "30");
                SortList.Add("3", "10");
                SortList.Add("4", "15");
                SortList.Add("5", "45");


                foreach (string item in listBox1.Items)
                {
                    string[] itemcol = item.Split(' ');   // splitting items by space
                    bool flag = true;

                    foreach (string str in itemcol)
                    {
                        if (SortList.ContainsKey(str))
                        {
                            string value = SortList[str].ToString();

                            if (int.Parse(value) >= 20)
                            {
                                //newArry.Add(item);

                            }
                            else
                            {
                                flag = false;
                                break;

                            }
                        }
                    }

                    if (flag)
                    {
                        foreach (string str in itemcol)
                        {
                            newArry.Add(item);
                        }
                    }
                }

and i dont quite get the second requirement .. can u please elaborate it further..??

(is this a sorting algorithm ? if ya then which one?)

If using the code u have gave, then the second requirement should be solved.. Just inside the

if (int.Parse(value) >= 20)
                            {
                                //put flag = true; ?
 
                            }

Problem had been solved, thank you very much sandeepparekh9 =)

Pls, close this thread if ur problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.