0

First, I have a string that contain all of characters and then I add it into Arraylist of list1 but I separate it with *. so now I have 5 elements of arraylist.

String fString = "DAVCFW_ACK*DAVCFW_20_30_90*DAVCFW_15.5_20.1_35.0*DAVCFW_40_230_110*DAVCFW_END";
ArrayList list1 = new ArrayList();
string[] words = fString.Split('*');
foreach (string s in words)
{
    list1.Add(s);
 }

Second, I delete first 6 charecters of each elements and keep it into Arraylist of list2.

ArrayList list2 = new ArrayList();
String s1 = list1.ToString();
foreach(string s in list1){
    for (int i = 0; i < s.Length; i++ ){
         if(i>6){
             list2[i-7] = list1[i];
         }
    }
}

Third, I delete all _ characters of each elemeant and keep it into Arraylist of list3. Now I get only number not characters.

ArrayList list3 = new ArrayList();
String[] word_s1 = s1.Split('_');
foreach(string s in word_s1){
     list3.Add(s);
}

And then I would like to keep the value of list3 like matrix but still is an Arraylist. I need to know number of element in each index of list3 to define row and colomn. For column I think I can use list3.count; but for row I don't know how. After I know row. I have to put 0 to element in each index that have number of row less than row.

Pleas Help me. Thanks you.

1
  • What kind of matrix? Is List<List<string>> good for you? Commented Sep 18, 2015 at 4:11

2 Answers 2

0

Did you ever run this code? First of all it doesn't work, because in second listing you are trying to get list1[7], where last index is 5. Second, in the same listing you are accesing element in subloop, not characters. Third, why did you do that String s1 = list1.ToString()? s1 is equal to string "System.Collections.ArrayList".

Back to your question. If you want to get length of every row, just use string.Length property. Here is working code:

String fString = "DAVCFW_ACK*DAVCFW_20_30_90*DAVCFW_15.5_20.1_35.0*DAVCFW_40_230_110*DAVCFW_END";
ArrayList list1 = new ArrayList();
string[] words = fString.Split('*');
foreach (string s in words)
{
    list1.Add(s);
}

ArrayList list2 = new ArrayList();
foreach(string s in list1)
{
    list2.Add(s.Substring(6));
}

ArrayList list3 = new ArrayList();
foreach (string s in list2)
{
    list3.Add(s.Replace("_", string.Empty));
}

foreach (string s in list3)
{
    Console.WriteLine(s);
}

Console.WriteLine("Number of rows: {0}", list3.Count);
for (int i = 0; i < list3.Count; i++)
{
    Console.WriteLine("Number of characters in {0} row: {1}",i, (list3[i] as string).Length);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you. And how about the column? :D
Wait, now I don't understand you question :) your rows are elements in list3, so list3.Count gives you number of rows. Every single element in List is your single row. Because they are strings you can get "column count" by accesing Length property. And you can manipulate with characters, so you can write logic to "put 0 to element in each index that have number of row less than row". If you want to have explicit column and rows, I would suggest using List<List<char>>
0

The question is not very clear about what you want. If you are after a matrix of item position as column, and values as rows, you can try something like this.

String fString = "DAVCFW_ACK*DAVCFW_20_30_90*DAVCFW_15.5_20.1_35.0*DAVCFW_40_230_110*DAVCFW_END";

Dictionary<int, ArrayList> matrix = new Dictionary<int,ArrayList>();
string[] words = fString.Split('*');
for (int i = 0; i < words.Length - 2 ; i++) // exclude the first and the last words because they do not have number
{
     string[] numbers = words[i+1].Substring(6).Split('_');
     ArrayList list = new ArrayList();
     for (int j = 1; j < numbers.Length; j++) // exlcude the first number as it is empty string
     {
         list.Add(numbers[j]);
     }
     matrix[i] = list;
}

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.