예제 #1
0
        public void PersonArray()
        {
            int[] lengths     = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            Array racers      = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            //setValue.
            racers.SetValue(new Person {
                FN = "A", LN = "B"
            }, index1: 1, index2: 10);
            //pay attention to the differences.
            racers.SetValue(new Person {
                FN = "c", LN = "d"
            }, 1, 11);
            racers.SetValue(new Person {
                FN = "c", LN = "d"
            }, 1, 12);
            racers.SetValue(new Person {
                FN = "c", LN = "d"
            }, 2, 10);
            racers.SetValue(new Person {
                FN = "c", LN = "d"
            }, 2, 11);
            racers.SetValue(new Person {
                FN = "c", LN = "d"
            }, 2, 12);

            Person[,] racer2 = (Person[, ])racers;
            Person first = racer2[1, 10];

            Console.Write("FisrtName:{0},LastName:{1}", racer2[1, 10].FN, racer2[1, 10].LN);
        }
예제 #2
0
        /*
         * Periods to which it is never possible to assign a mentor (i.e., no mentor is available)
         * UnassignablePeriods[d,p] == true => the period (d,p) has no available mentor
         */
        bool[,] UnassignablePeriods(List <Person> unassignedMentors, Person[,] assignedPeriods)
        {
            bool[,] unassignable = new bool[5, 10];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    unassignable[i, j] = true;
                }
            }

            foreach (Person mentor in unassignedMentors)
            {
                for (int d = 0; d < 5; d++)
                {
                    for (int p = 0; p < 10; p++)
                    {
                        if (unassignable[d, p] && assignedPeriods[d, p] != null || mentor.GetAvailable(d, p))
                        {
                            unassignable[d, p] = false;
                        }
                    }
                }
            }

            return(unassignable);
        }
예제 #3
0
        static void ArrayClass()
        {
            Array intArray1 = Array.CreateInstance(typeof(int), 5);

            for (int i = 0; i < 5; i++)
            {
                intArray1.SetValue(33, i);
            }
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(intArray1.GetValue(i));
            }

            int[] lengths     = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            Array racers      = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            racers.SetValue(new Person("Alain", "Prost"), 1, 10);
            racers.SetValue(new Person("Emerson", "Fittipaldi"), 1, 11);

            racers.SetValue(new Person("Ayrton", "Senna"), 1, 12);
            racers.SetValue(new Person("Michael", "Schumacher"), 2, 10);
            racers.SetValue(new Person("Fernando", "Alonso"), 2, 11);
            racers.SetValue(new Person("Jenson", "Button"), 2, 12);

            Person[,] racers2 = (Person[, ])racers;
            Person first = racers2[1, 10];
            Person last  = racers2[2, 12];
        }
예제 #4
0
 public Scheduler(List <Person> mentors, List <Person> mentees)
 {
     unassignedMentees = new List <Person>(mentees);
     unassignedMentors = new List <Person>(mentors);
     assignedPeriods   = new Person[5, 10]; // 5 days with 10 periods each
     menteesAssigned   = new List <Person> [5, 10];
     mentorsAssigned   = new List <Person> [5, 10];
     // initialize a list for every period
     for (int i = 0; i < 5; i++)
     {
         for (int j = 0; j < 10; j++)
         {
             menteesAssigned[i, j] = new List <Person>();
             mentorsAssigned[i, j] = new List <Person>();
         }
     }
 }
예제 #5
0
파일: Program.cs 프로젝트: zilo312/aa
        static void CreateUsingArrayClass()
        {
            Array intArray = Array.CreateInstance(typeof(int), 5);

            Array intArray1 = Array.CreateInstance(typeof(int), 5);

            for (int i = 0; i < 5; i++)
            {
                intArray1.SetValue(33, i);
            }

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(intArray1.GetValue(i));
            }


            int[] ia = (int[])intArray;

            int[] lengths     = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            Array racers      = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            racers.SetValue(new Person("Alain", "Prost"), 1, 10);
            racers.SetValue(new Person("Emerson", "Fittipaldi"), 1, 11);
            racers.SetValue(new Person("Ayrton", "Senna"), 1, 12);
            racers.SetValue(new Person("Ralf", "Schumacher"), 2, 10);
            racers.SetValue(new Person("Fernando", "Alonso"), 2, 11);
            racers.SetValue(new Person("Jenson", "Button"), 2, 12);


            Person[,] p2 = (Person[, ])racers;

            p2[1, 10] = new Person("x", "y");
            p2[1, 12] = new Person("a", "b");
            p2[3, 10] = new Person("b", "c");
            p2[3, 13] = new Person("x", "y");
            foreach (Person p in racers)
            {
                Console.WriteLine(p);
            }

            //   int[] k = new int[2];
        }
예제 #6
0
        //Contructor
        public Console2D()
        {
            grid          = new char[10, 10];
            personGrid    = new Person[10, 10];
            soldierGrid   = new Soldier[10, 10];
            knightGrid    = new Knight[10, 10];
            gridToDisplay = new char[10, 10];


            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    grid[i, j]          = 'X';
                    gridToDisplay[i, j] = '?';
                }
            }
            this.initializeChar();
        }
예제 #7
0
        public Scheduler(List <string> mentors, List <string> mentees)
        {
            // generate People objects from the mentors and mentees supplied
            unassignedMentees = new List <Person>();
            unassignedMentors = new List <Person>();
            foreach (string m in mentors)
            {
                Person p = new Logic.Person(Role.Mentor);
                p.ID = m;
                foreach (Tuple <int, int> freePeriod in DatabaseUtilities.GetFreePeriodsForUser(p.ID))
                {
                    p.SetAvailable(freePeriod.Item1, freePeriod.Item2, true);
                }
                unassignedMentors.Add(p);
            }

            foreach (string m in mentees)
            {
                Person p = new Logic.Person(Role.Mentee);
                p.ID = m;
                foreach (Tuple <int, int> freePeriod in DatabaseUtilities.GetFreePeriodsForUser(p.ID))
                {
                    p.SetAvailable(freePeriod.Item1, freePeriod.Item2, true);
                }
                unassignedMentees.Add(p);
            }

            assignedPeriods = new Person[5, 10]; // 5 days with 10 periods each
            menteesAssigned = new List <Person> [5, 10];
            mentorsAssigned = new List <Person> [5, 10];
            // initialize a list for every period
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    menteesAssigned[i, j] = new List <Person>();
                    mentorsAssigned[i, j] = new List <Person>();
                }
            }
        }
예제 #8
0
        public static void Search(int level, ref Person[] R, ref bool HAVE, int[] S, Person[,] P)
        {
            int i = -1;

            while (!HAVE && i < S[level])
            {
                i++;
                if (Ft(level, P[level, i]))
                {
                    if (Fk(R, level, P[level, i]))
                    {
                        R[level] = P[level, i];
                        if (level == P.GetLength(0) - 1)
                        {
                            HAVE = true;
                        }
                        else
                        {
                            Search(level + 1, ref R, ref HAVE, S, P);
                        }
                    }
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Clears out the square states and all associated objects.
        /// Sets them all to the specified state.
        /// </summary>
        protected void InitializeSquareStates()
        {
            BuildingInteractionType startMode;
            if (NumberOfMaterialsPerSquare == 0)
                startMode = BuildingInteractionType.BUILD;
            else if (NumberOfMaterialsPerSquare > 0)
                startMode = BuildingInteractionType.LOAD_BUILDING_MATERIALS;
            else
                throw new NotImplementedException();

            squareActionModes = new BuildingInteractionType[Width, Height];
            squareAvailabilityModes = new BuildingAvailabilityType[Width, Height];

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    squareActionModes[x, y] = startMode;
                    squareAvailabilityModes[x, y] = BuildingAvailabilityType.AVAILABLE;
                }
            }

            modeCounts = new Dictionary<Tuple<BuildingInteractionType, BuildingAvailabilityType>, int>();

            foreach (BuildingAvailabilityType bat in Enum.GetValues((typeof(BuildingAvailabilityType))))
            {
                foreach (BuildingInteractionType bit in Enum.GetValues((typeof(BuildingInteractionType))))
                {
                    modeCounts[Tuple.Create(bit, bat)] = 0;
                }
            }

            modeCounts[Tuple.Create(startMode, BuildingAvailabilityType.AVAILABLE)] = Area;

            markers = new FullTask[Width, Height];
            occupants = new InWorldObject[Width, Height];
            users = new Person[Width, Height];

            materials = new InWorldObject[Width, Height, NumberOfMaterialsPerSquare];
            materialIndices = new int[Width, Height];
        }
예제 #10
0
파일: Program.cs 프로젝트: liuqun/csharpdev
        static void Main(string[] args)
        {
            // 创建使用从0开始的索引,具有指定Type类型,Length长度的一维Array
            Array intArray1 = Array.CreateInstance(typeof(int), 5);

            for (int i = 0; i < 5; i++)
            {
                // 将某值value设置给一维Array中指定位置index处的元素
                intArray1.SetValue(33, i);
            }
            for (int i = 0; i < 5; i++)
            {
                // 获取一维Array中指定位置index处的元素
                Console.WriteLine(intArray1.GetValue(i));
            }

            // CreateInstance()也可以创建多维数组和不基于0的数组
            int[] lengths     = { 2, 3 };  // 多维Array的维长
            int[] lowerBounds = { 1, 10 }; // 多维Array的索引下限
            // 创建不从0开始的索引,具有指定Type类型,索引下限lowerBounds,维长lengths的多维Array
            Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            // 将某对象object设置给二维Array中指定索引下限lowerBound开始,指定维长lengths处
            racers.SetValue(new Person
            {
                FirstName = "Alain",
                LastName  = "Prost"
            }, index1: 1, index2: 10);
            racers.SetValue(new Person
            {
                FirstName = "Emerson",
                LastName  = "Fittipaldi"
            }, index1: 1, index2: 11);
            racers.SetValue(new Person
            {
                FirstName = "Ayrton",
                LastName  = "Senna"
            }, index1: 1, index2: 12);
            racers.SetValue(new Person
            {
                FirstName = "Michael",
                LastName  = "Schumacher"
            }, index1: 2, index2: 10);
            racers.SetValue(new Person
            {
                FirstName = "Fernando",
                LastName  = "Alonso"
            }, index1: 2, index2: 11);
            racers.SetValue(new Person
            {
                FirstName = "Jenson",
                LastName  = "Button"
            }, index1: 2, index2: 12);
            // 尽管数组不是基于0,但可以使用一般的C#表示法将它赋予一个变量
            // 将Array类型转换为Person[,]数组类型
            Person[,] racers2 = (Person[, ])racers;
            Console.WriteLine("racers2[1,10] = " + racers2[1, 10]);
            Console.WriteLine("racers2[2,12] = " + racers2[2, 12]);

            Console.Read();
        }
예제 #11
0
        static void Main(string[] args)
        {
            /*int[] myArray1 = new int[4] { 4, 7, 11, 2 };
             * int[] myArray2 = new int[] { 4, 7, 11, 2 };
             * int[] myArray3 = { 4, 7, 11, 2 };
             * int v1 = myArray1[0];//通过索引器传递元素编号,就可以访问数组
             * myArray3[3] = 44;
             * for(int i=0;i<myArray3.Length;i++)
             * {
             *  Console.Write(myArray3[i]);
             * }
             * Console.WriteLine();
             * foreach(var yuyaoyao in myArray3)
             * {
             *  Console.WriteLine(yuyaoyao);
             * }
             *
             * Person[] person = new Person[2];
             * person[0] = new Person { FirstName = "cjh", LastName = "yyy" };
             * person[1] = new Person { FirstName = "ckx", LastName = "cyx" };
             * Person[] persons =
             *  {
             *       new Person { FirstName = "cjh", LastName = "yyy" },
             *       new Person { FirstName = "ckx", LastName = "cyx" }
             *  };
             *
             * //二维数组
             * int[,] twodim = new int[2, 2];
             * twodim[0, 0]= 1;
             * twodim[0, 1] = 2;
             * twodim[1, 0] = 3;
             * twodim[1, 1] = 4;
             * int[,] Twodim =
             * {
             *  {1,2},
             *  {3,4}
             * };
             *
             * //锯齿数组
             * int[][] jag = new int[3][];
             * jag[0] = new int[2] { 1, 2 };
             * jag[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
             * jag[2] = new int[3] { 9, 10, 11 };
             * for(int i=0;i<jag.Length; i++)
             * {
             *  for(int j=0;j<jag[i].Length;j++)
             *  {
             *      Console.WriteLine("行: " + i + "列: " + j + "值: " + jag[i][j]);
             *  }
             * }*/

            Array array = Array.CreateInstance(typeof(int), 5);

            for (int i = 0; i < 5; i++)
            {
                array.SetValue(33, i);
            }
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(array.GetValue(i));
            }
            int[] array1      = (int[])array;//将已创建的数组强制转换成声明为int[]的数组
            int[] lengths     = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            Array racers      = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            racers.SetValue(new Person {
                FirstName = "Alain", LastName = "Prost"
            }, index1: 1, index2: 10);
            racers.SetValue(new Person {
                FirstName = "Limi", LastName = "John"
            }, index1: 1, index2: 11);
            racers.SetValue(new Person {
                FirstName = "Derek", LastName = "Amy"
            }, index1: 1, index2: 12);
            racers.SetValue(new Person {
                FirstName = "Shay", LastName = "Funy"
            }, index1: 2, index2: 10);
            racers.SetValue(new Person {
                FirstName = "Zang", LastName = "Kang"
            }, index1: 2, index2: 11);
            racers.SetValue(new Person {
                FirstName = "Uiny", LastName = "Qing"
            }, index1: 2, index2: 12);
            Person[,] racers2 = (Person[, ])racers;
            Person first = racers2[1, 10];
            Person last  = racers2[2, 12];

            Console.WriteLine(first + "  " + last);

            int[] intArray1 = { 1, 2 };//复制数组
            int[] intArray2 = (int[])intArray1.Clone();
            for (int n = 0; n < intArray2.Length; n++)
            {
                Console.WriteLine(intArray2[n]);
            }

            Person[] beatles =
            {
                new Person {
                    FirstName = "Zang", LastName = "Kang"
                },
                new Person {
                    FirstName = "Uiny", LastName = "Qing"
                }
            };//beatles和beatlesClone引用的Person对象是相同的,如果修改beatlesClone中一个元素的属性,就会改变beatles中的对应对象
            Person[] beatlesClone = (Person[])beatles.Clone();

            string[] names =
            {
                "Christina Aguilera",
                "Shakira",
                "Beyonce",
                "Gwen Stefani"
            };

            Array.Sort(names);

            foreach (string name in names)
            {
                Console.WriteLine(name);
            }

            /* People[] peoples =
             * {
             * new People { FirstName = "Alain", LastName = "Prost" },
             * new People { FirstName = "Limi", LastName = "John" },
             * new People { FirstName = "Derek", LastName = "Amy" },
             * new People { FirstName = "Shay", LastName = "Funy" }
             * };
             * Array.Sort(peoples);
             * foreach(var p in peoples)
             * {
             * Console.WriteLine(p);
             * }
             *
             * Array.Sort(peoples, new PeopleComparer(PeopleCompareType.FirstName);
             * foreach(var P in peoples)
             * {
             * Console.WriteLine(P);
             * }*/

            int[] ar1      = { 1, 4, 5, 11, 13, 18 };
            int[] ar2      = { 3, 4, 5, 18, 21, 27, 33 };
            var   segments = new ArraySegment <int>[2]
            {
                new ArraySegment <int>(ar1, 0, 3),
                new ArraySegment <int>(ar2, 3, 3)
            };
            var sum = SumOfSqgments(segments);

            Console.WriteLine("sum of all segments: {0}", sum);


            var         game       = new GameMoves();
            IEnumerator enumerator = game.Cross();

            while (enumerator.MoveNext())
            {
                enumerator = enumerator.Current as IEnumerator;
            }

            var result = Divide(5, 2);

            Console.WriteLine("result of division:{0},reminder:{1}", result.Item1, result.Item2);

            var tuple = Tuple.Create <string, string, string, int, int, int, double, Tuple <int, int> > ("Stephanie", "Alina", "Nagel", 2009, 6, 2, 1.37, Tuple.Create <int, int>(52, 3490));

            Console.WriteLine(tuple.Item2);

            var janet = new Per {
                FirstName = "Janet", LastName = "Jackson"
            };

            Per[] per1 =
            {
                new Per
                {
                    FirstName = "Michael",
                    LastName  = "Jackson"
                }, janet
            };
            Per[] per2 =
            {
                new Per
                {
                    FirstName = "Michael",
                    LastName  = "Jackson"
                }, janet
            };
            if (per1 != per2)
            {
                Console.WriteLine("not the same reference");
            }
            if ((per1 as IStructuralEquatable).Equals(per2, EqualityComparer <Per> .Default))
            {
                Console.WriteLine("the same content");
            }
            var t1 = Tuple.Create <int, string>(1, "Stephanie");
            var t2 = Tuple.Create <int, string>(1, "Stephanie");

            if (t1 != t2)
            {
                Console.WriteLine("not the same reference to the tuple");
            }
            if (t1.Equals(t2))
            {
                Console.WriteLine("the same content");
            }

            /*if (t1.Equals(t2, new TupleComparer()))
             *  Console.WriteLine("equals using TupleComparer");*/
        }
예제 #12
0
        /*数组可以支持协变,但协变只能支持引用类型,不适用值类型。
         * 协变只能在运行时才能发现错误。
         */
        public void TestArray()
        {
            Person[] myPersons = new Person[2];
            myPersons[0] = new Person {
                FirstName = "Jun", SecondName = "Wang"
            };
            myPersons[1] = new Person {
                FirstName = "Rui", SecondName = "Yang"
            };
            Console.WriteLine(myPersons[0].ToString() + "\n" + myPersons[1].ToString());

            Person[] myPersons2 =
            {
                new Person {
                    FirstName = "James", SecondName = "Zhang"
                },
                new Person {
                    FirstName = "Tomas", SecondName = "SiMa"
                }
            };

            Console.WriteLine(myPersons2[0].ToString() + "\n" + myPersons2[1].ToString());

            //多维数组
            int[,] twoDim =
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            };

            int[,,] threeDim =
            {
                { { 1,  2 }, {  3,  4 } },
                { { 5,  6 }, {  7,  8 } },
                { { 9, 10 }, { 11, 12 } }
            };

            //锯齿数组(交叉数组)
            int[][] jagged = new int[3][];
            jagged[0] = new int[2] {
                1, 2
            };
            jagged[1] = new int[5] {
                3, 4, 5, 6, 7
            };
            jagged[2] = new int[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            };

            for (int row = 0; row < jagged.Length; row++)
            {
                for (int element = 0; element < jagged[row].Length; element++)
                {
                    Console.WriteLine("Row:{0},Element:{1},Value:{2}", row, element, jagged[row][element]);
                }
            }

            //Array类
            Array intArray1 = Array.CreateInstance(typeof(int), 5);

            for (int i = 0; i < 5; i++)
            {
                intArray1.SetValue(33, i);
            }

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(intArray1.GetValue(i));
            }

            //强制转换成int数组
            int[] intArray2 = (int[])intArray1;
            //CreateInstance创建一个2x3的数组
            int[] lengths     = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            //第一维基于1,第二维基于10
            Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

            racers.SetValue(new Person {
                FirstName = "AAA", SecondName = "111"
            }, 1, 10);
            racers.SetValue(new Person {
                FirstName = "BBB", SecondName = "222"
            }, 1, 10);
            racers.SetValue(new Person {
                FirstName = "CCC", SecondName = "333"
            }, 1, 11);
            racers.SetValue(new Person {
                FirstName = "DDD", SecondName = "444"
            }, 1, 12);
            racers.SetValue(new Person {
                FirstName = "EEE", SecondName = "555"
            }, 2, 10);
            racers.SetValue(new Person {
                FirstName = "FFF", SecondName = "666"
            }, 2, 11);
            racers.SetValue(new Person {
                FirstName = "GGG", SecondName = "777"
            }, 2, 12);

            Person[,] racers2 = (Person[, ])racers;
            Person first  = racers2[1, 10];
            Person second = racers2[2, 12];

            //Clone()方法会创建数组的浅层副本,如果元素是值类型,会复制所有值,修改原数组内容副本内容不会变化
            int[] cloneArray1 = new int[] { 1, 2, 3 };
            int[] cloneArray2 = (int[])cloneArray1.Clone();
            cloneArray1[0] = 100;
            Console.WriteLine(cloneArray2[0]);             //输出1

            //如果元素是引用类型,则会复制元素的引用。
            Person[] clonePersons = (Person[])myPersons.Clone();
            myPersons[0].FirstName  = "000";
            myPersons[0].SecondName = "VVV";
            //修改元素内容副本元素的内容也会变化
            Console.WriteLine(clonePersons[0].ToString());            //输出:000 VVV
            //重置原数组元素副本内容不会受影响
            myPersons[0] = new Person {
                FirstName = "MMM", SecondName = "---"
            };
            Console.WriteLine(clonePersons[0].ToString());             //输出:000 VVV

            /*Copy()方法也会创建浅层复制,区别在于:
             * Copy()方法:必须传递阶数相同且有足够元素的已有数组。
             * Clone()方法:会创建一个新数组。
             * 如果需要创建包含引用类型的数组的深层副本,就必须迭代数组病创建新对象。
             */
        }