Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i want to add value of a variable to a list and then change the value,
for example:

static List<int[]> list = new List<int[]>();

int[] piece = { 2, 6, 0, 7, 4, 1, 5, 3 };
list.Add(piece);

piece = ShuffleArray(piece);

list.Add(piece); ------> replace with last array!!!!!!!

static int[] ShuffleArray(int[] array)
{
Random r = new Random();
for (int i = array.Length; i > 0; i--)
{
int j = r.Next(i);
int k = array[j];
array[j] = array[i - 1];
array[i - 1] = k;
}
return array;
}



but when i change the value of piece the value of list will change.
please help me.
Posted
Updated 12-Jan-15 20:50pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Jan-15 2:31am    
This is not list of integers, this is a list of arrays. Why? What do you want to achieve?
—SA
mohamad atrian 13-Jan-15 2:45am    
yes!! i want to save arrayes in list to remove repetitive after that
Sergey Alexandrovich Kryukov 13-Jan-15 2:51am    
Not clear why, but I answered your question. Can you see now how it works?
—SA
Member 11253892 13-Jan-15 3:01am    
i want to declare new value with function: line 4
Sergey Alexandrovich Kryukov 13-Jan-15 10:41am    
And?..
—SA

Please see my comment to the question.
There is no a problem to replace element

C#
list[index] = new int[] {10, 11, 12, }; // for example
// or
(list[index + 1])[3] = 19;


One statement replaces the element of the list with the new array, another one leaves the same list element (which is the same array), but modifies the array's element.

—SA
 
Share this answer
 
piece = ShuffleArray(piece.Clone());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900