更改列表项后,C#的值发生了变化

时间:2017-12-16 16:39:31

标签: c# list

我有这段代码,更改了List1项目后,double [] X中的元素发生了变化

    public class Class1
    {
        double[] C;
        double[][] pos;   
        public double S;
        double[] X;
        List<double[][]> List1 = new List<double[][]>();

        public Class1()
        {
        }

        public void runCode()
        {

            pos = new double[10][];
            for (int ii = 0; ii < 10; ii++)
            {
                pos[ii] = new double[5];
                for (int jj = 0; jj < 5; ++jj)
                    pos[ii][jj] = 0;
            }

            List1 = ListExtensions.ChunkBy(pos, 1);
            #region 
            X = new double[5];

            for (int jj = 0; jj < 5; ++jj)
            {
                X[jj] = 0.0;
            }

            S = 10000000000000000000;
            #endregion

            C = new double[List1[0].Length];
            for (int ii = 0; ii < List1[0].Length; ++ii)
            {
                for (int jj = 0; jj < 5; ++jj)
                    List1[0][ii][jj] = 1;


                if (C[ii] < S)
                {
                    X = List1[0][ii];
                }

            }
            //======================= Before
            for (int jj = 0; jj < 5; ++jj)
                Console.WriteLine("----------------    " + X[jj]);


            Console.WriteLine("\n");
            for (int ii = 0; ii < List1[0].Length; ++ii)
               for (int jj = 0; jj < 5; jj++)
                   List1[0][ii][jj] = 5;

             Console.WriteLine("//======================= After");

            for (int jj = 0; jj < 5; ++jj)
                Console.WriteLine("----------------    " + X[jj]);

            Console.Read();
        }  
    } // end class   
}

// --------------------------------------------- ----

public static class ListExtensions
    {
        public static List<double[][]> ChunkBy(double[][] x, int number)
        {
            var result = new List<double[][]>();
            int chunkSize = (int)Math.Ceiling(((double)x.GetLength(0)) / 
            number); 
            for (int i = 0; i < number; i++)
            {
                result.Add(x.Skip(chunkSize * i).Take(chunkSize).ToArray());
            }

            return result;
         }
    }

// --------------------------------------------- ----------------------------

static void Main(string[] args)
{
 Class1 test = new Class1();
           test.runCode();
}

仅在此部分中更改X的值:

 if (C[ii] < S)
 {
    X = List1[0][ii];
 }

输出:

---------------- 1

---------------- 1

---------------- 1

---------------- 1

---------------- 1

// =======================

之后

---------------- 5

---------------- 5

---------------- 5

---------------- 5

---------------- 5

for循环后double [] X中的元素应为1而不是5

1 个答案:

答案 0 :(得分:0)

使用 List1 [0] [ii] .CopyTo(X,0);而不是X = List [0] [ii];`

制作List1 [0] [ii]的副本并将其保存在double [] X

if(isset($_POST["submit"])) 
    {
        $path = "images/$user_id";

        if(!file_exists($path))
        {
            mkdir($path, 0755, true);
        }

        $target_dir = "$path/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) 
        {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } 
        else 
        {
            echo "File is not an image.";
            $uploadOk = 0;
        }

        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
        {
            chmod($path . "/" . $_FILES["fileToUpload"]["name"], 0755); 
            echo "The file was upload successfully.";   
        }
        else
        {
            echo "There was an error with the upload.";
        }
    }