Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 371.2k

How to load Arraylist to Array

Jan 5 2017 4:29 AM
I have a Array list.I want to convert this into separate array.
My Array list name is [obj];
 
obj={  {[A],[B]} ,
          {[C],[D]},
          {[E],[F]}  }
 
Now I want to convert  it to separate Array.
array x1= {[A],[B]};
array x2= {[C],[D]};
array x3=  {[E],[F]};
 
What am tried:
  1. for (var i = 0; i < obj.length; i++) {  
  2. X = obj[i];  
  3. }  
 Please help me to achieve this?

Answers (9)

5
Suthish Nair

Suthish Nair

  • 0
  • 30.5k
  • 7.2m
Jan 5 2017 4:59 AM
string[] array will have the collection... you can use array[0]... array[i]
4
Khaja Moizuddin

Khaja Moizuddin

  • 161
  • 11.5k
  • 1.9m
Jan 5 2017 11:59 AM
Try this https://msdn.microsoft.com/en-us/library/fcyyh2hb(v=vs.110).aspx https://www.dotnetperls.com/convert-arraylist-array http://stackoverflow.com/questions/8924869/how-to-convert-arraylist-into-string-arraystring-in-c-sharp hope this helps
4
Bikesh Srivastava

Bikesh Srivastava

  • 81
  • 23.9k
  • 3.5m
Jan 5 2017 11:50 AM
i think best option is use foreach loop and store one by one in separte array.
4
Dinesh Santhalingam

Dinesh Santhalingam

  • 0
  • 737
  • 371.2k
Jan 5 2017 4:56 AM
Thanks for your guidance.@

Chetan Ranpariya

4
Chetan Ranpariya

Chetan Ranpariya

  • 0
  • 1.1k
  • 240
Jan 5 2017 4:49 AM
Dinesh,
 
You can not generate the variable names at runtime like X1, X2 and so on.
 
Here you have obj, which is Array of Arrays.
Instead of creating another array out of it why don't you use it directly where ever you want to use it? As following. 
  1. for (var i = 0; i < obj.length; i++) {    
  2.     var arr = obj[1];  
  3.     //do looping with arr to access its values  
  4.     for(var j = 0; j< arr.length;j++)  
  5.     {  
  6.         //use arr[i] to do whaterver you want...  
  7.     }  
  8. }  
I hope this will help you.
 
This is a simple logic.
 
Thanks and regards,
Chetan Ranpariya
[No programming language is helpful if logic is not clear] 
 
 
4
Dinesh Santhalingam

Dinesh Santhalingam

  • 0
  • 737
  • 371.2k
Jan 5 2017 4:41 AM
Can you please explain more about  what the function doing? @

Suthish Nair

4
Dinesh Santhalingam

Dinesh Santhalingam

  • 0
  • 737
  • 371.2k
Jan 5 2017 4:39 AM
The last array in array list only append to the X.I want to create X1,X2,X3 arrays and append that three arrays from Arraylist @

Chetan Ranpariya

4
Suthish Nair

Suthish Nair

  • 0
  • 30.5k
  • 7.2m
Jan 5 2017 4:37 AM
  1. string[] array = obj.ToArray(typeof(string)) as string[];  
  2. or
  3. string[] array = (string[])obj.ToArray(typeof(string));
4
Chetan Ranpariya

Chetan Ranpariya

  • 0
  • 1.1k
  • 240
Jan 5 2017 4:36 AM
Hi Dinesh,
 
What problem did you face with your approach?
 
Did you get any error or exception?
 
Thanks and regards,
Chetan Ranpariya