您正在使用过期的浏览器。它可能无法正确显示此或其他网站。
您应该升级或使用 替代浏览器.

问题  记录类型阵列/列表

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
你好
我的名字是坦率与唱片类型阵列/列表(Pascal编程类比)挣扎。我正在尝试构建记录类型数组或列表,而是作为初学者,我不明白它是如何运作的,特别是通过Reline或Row访问它,使用它,显示它。我也在考虑数据表选项,但我需要解决这个问题。我在下面发布我的实例,请帮帮我谢谢,弗兰克

consoleapp2listtyplist1:
using System;
using System.Collections.Generic;
using System.Collections;

namespace ConsoleApp2listtyplist1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Collection of string 
            string[] animals = { "bird 1", "bird 1", "bird 1" };
            string[] animals2 = { "bird 12", "bird 12", "bird 12" };
            string[] animals3 = { "bird 13", "bird 13", "bird 13" };
            // Create a List and add a collection 
            List<string> animalsList = new List<string>();
            animalsList.AddRange(animals);
            animalsList.AddRange(animals2);
            animalsList.AddRange(animals3);
            foreach (string a in animalsList)
                Console.WriteLine(a);

            ArrayList a1 = new ArrayList();
            a1.Add(1);
            a1.Add("Example");
            a1.Add(true);

            Console.WriteLine(a1[0]);
            Console.WriteLine(a1[1]);
            Console.WriteLine(a1[2]);
            Console.ReadKey();
            Console.WriteLine("Hello World!");

            List<string> array1 = new List<string>();
            array1.Add("test");

            List<List<string>> array2 = new List<List<string>>();
            array2.Add(array1);

            List<ArrayList> alsList = new List<ArrayList>();
        }
    }
}
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
The C# equivalents to the Pascal RECORD type is are classes and struct. In Pascal, if you wanted to have an array of strings inside the RECORD, you would declare it inside the the RECORD. The same is true for C#. If you want an array of strings in a class or struct, you would declare it inside as well. And similar to Pascal, if you want an array record types, you would declare an array of that record type, in C# you would also declare and array of the class or struct type.
 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,731
地点
悉尼,澳大利亚
编程经验
10+
一般来说,您只会在类中使用阵列进行内部使用。如果要在对象外公开任何数据,那么您应该使用属性,如果要公开数据列表,那么您应该使用集合类型的只读属性。这样,消费者可以获得现有的集合才能获得,设置,添加和删除项目,但它们无法完全替换现有的集合。例如。
C#:
public class Person
{
    public string GivenName { get; set; }
    public string FamilyName { get; set; }
    public List<Person> Children { get; } = new List<Person>();
}
Here, the Person class has two basic properties for the person's given and family names and a read-only collection property that contains other Person objects that represent that person's children. You might use that like this:
C#:
// It's OK to use an array here because it is for internal use.
var people = new[]
             {
                 new Person {GivenName = "Peter", FamilyName = "Smith"},
                 new Person {GivenName = "Paul", FamilyName = "Jones"},
                 new Person {GivenName = "Mary", FamilyName = "Williams"}
             };

var peter = people[0];

peter.Children.AddRange(new[]
                        {
                            new Person {GivenName = "John", FamilyName = "Smith"},
                            new Person {GivenName = "Jane", FamilyName = "Smith"}
                        });

foreach (var person in people)
{
    Console.WriteLine($"{person.GivenName} {person.FamilyName}");

    if (person.Children.Any())
    {
        Console.WriteLine("Children:");

        foreach (var child in person.Children)
        {
            Console.WriteLine($"{child.GivenName} {child.FamilyName}");
        }
    }
    else
    {
        Console.WriteLine("No children");
    }
}
In this case, the collection property in the Person class also contains Person objects, but your types can refer to any other types you like. For instance, the DataTable class has a Columns property that is a collection of DataColumn objects and a Rows property that is a collection of DataRow objects.
 

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
谢谢你的帮助
我有另一个问题,转换。我需要在下面的代码中得到帮助。我需要将数组元素匹配到一个字符串,找到匹配字符,ABC包含英语alfabeth,str2 应该mathc the alfabet.thank你,弗兰克。

matching elements:
 foreach (char c in abc)
                {
                    //Console.WriteLine(c.ToString());
                    //charVal = Convert.ToChar(str2[i]);
                    charVal = str2[i];
                    if (charVal == str2[i].ToString()) { Console.WriteLine(c.ToString());}
                }
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
Assuming that str2 is declared as a string, then str2[i] is a char. So in the code in post #4, presumably charValue is declared to be a char. Calling ToString() on a char will return a string. As previously mentioned in your 其他线程, you can't compare a char to a string.

Why do you keep on converting things to string by calling ToString()? A string is already a string, so there no need to convert it to a string. Console.WriteLine() has an override that takes a char parameter, so there is no need to convert a char to a string. By any chance was JavaScript your first programming language, or are you learning C# and JavaScript at the same time?
 

约翰

C#论坛主持人
工作人员
加入
2011年4月23日
消息
1,160
地点
挪威
编程经验
10+

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
Assuming that str2 is declared as a string, then str2[i] is a char. So in the code in post #4, presumably charValue is declared to be a char. Calling ToString() on a char will return a string. As previously mentioned in your 其他线程, you can't compare a char to a string.

Why do you keep on converting things to string by calling ToString()? A string is already a string, so there no need to convert it to a string. Console.WriteLine() has an override that takes a char parameter, so there is no need to convert a char to a string. By any chance was JavaScript your first programming language, or are you learning C# and JavaScript at the same time?
我认为这是最好地发布整个代码,在哪里可以看到问题。
这是一个家庭工作,我正在服用C#编程课程。
第一个prog。语言帕斯卡尔,然后我在伦敦英国开始C ++,但Covid的东西杀了它,也有点蟒蛇。此外,我已经学习了jQuery,开始使用FreeCodecamp的JavaScript,但由于本课程,我必须重新排列我的优先顺序,所以我发布了我需要找到所要求的家庭电话号码的可能颜色的代码,与邻居不同。在这里,我使用englis alfabeth中的字符串中的颜色“名称”。
sitreadmin:
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace ConsoleApp2kerites1
{
    class Program
    {
        public class Listofrec
        {
            public string szin { get; set; }
            public string hazszam  {get; set;}
        }
        static void Main(string[] args)
        {
            //--------------------
            //File írás és olvasás
            //--------------------
            int i =0;
            int index1 = 0;
            int index2 = 0;
            int i2 =0;
            int i3 = 0;
            int j =0;
            int k =0;
            //string l;
            Random r = new Random();
            //int genRand = r.Next(1, 59);
            int genRand = r.Next(1, 117);
            string szin = " ";
            string szinlehetoseg = " ";
            string ch = " ";
            string abc ="abcdefghijklmnopqrstuvxyz";
            // Declaration of the array
            string[] str1;
            string[] str2;
            str1 = new string[99];
            str2 = new string[99];
            int paros = 0;
            int paratlan = -1;
            int hszam1 = 0;
            int hszamkert = 0;
            string keritesszin = " ";
            //string adat;
            string name = @"c:\Users\Public\textfiles\kerites.txt";
            Listofrec[] adatok = new Listofrec[99];
            //var adatok = new List<Listofrec>();
            //string line;
            //string str= '9';
            /*string path = @"c:\\Users\\Dell\\Desktop\\c#\\kerites.txt";*/
            StreamReader st = new StreamReader(name, Encoding.Default);

            string s;
            string[] words;
            //string[] kerites;
            //var cls = new Listofrec();
            //adatok[1].hazszam ="0";
            //adatok[1].szin = "b";
            Listofrec[] listak = new Listofrec[99];
             Listofrec list1 = new Listofrec()
            {
                hazszam = "0",
                szin = "b"
            };
            listak[0] = list1;
             char charVal;

            //cls.Add(adatok);

            do
            {
                i = i + 1;
                i2 = i2 + 1;

                s = st.ReadLine();

                // i2 = i2 + 1;
                str1[i] = s;
                //string[] words = s.Split(' ');
                //string[i,] words = s.Split(' ');
                words = s.Split(' ');
                str2[i] = words[2];
                Console.WriteLine("- i " + i + "- " + words[0] + "- " + words[1] + "- " + words[2]);
                Console.WriteLine(" lista " + i + " " + str1[i]);
                //if (words[0]==0) {
                //    paros = paros + 1;
                //}[]
                if (Int32.TryParse(words[0], out j))
                {
                    //paros = paros + 1;

                    if (j == 0)
                    {
                        paros = paros + 2;
                        index1 = index1 + 1;
                        Console.WriteLine(j);
                        Console.WriteLine(" Paros szamok " + paros);
                    }
                    if (j == 1)
                    {
                        paratlan = paratlan + 2;
                        index2 = index2 + 1;
                        /*  if (genRand==index2)
                             {
                                  hszamkert = paratlan;
                                  szin = str2[i];
                                  Console.WriteLine(index2+" A kert haz szam " + hszamkert+szin);

                              }*/
                        Console.WriteLine(index2 + " Paratlan szamok " + paratlan);
                    }
                    // Console.WriteLine(" Az utolso szam paros " + paros);

                }

                else
                {
                    Console.WriteLine("String could not be parsed.");
                }

                /* */
                if (j == 1)
                {
                    //
                    if (i > 1)
                    {
                        if (str2[i] == str2[i - 1])
                        {
                            if (str2[i] != "#" && str2[i] != ":")
                            {
                                k = k + 1;
                                Console.WriteLine(i2 + " A szomszeddal egyezo szin " + str2[i]);
                                //adatok[i].hazszam.Add(str2[i]);
                                //adatok[i].szin.Add(paratlan);


                                if (k < 2)
                                {
                                    hszam1 = paratlan;
                                    szin = str2[i];
                                    Console.WriteLine(" A szomszeddal egyezo hazszam " + hszam1);
                                  
                                    Console.WriteLine(" A szomszeddal egyezo kerites szin " + szin + "  hazszam : " + paratlan);
                                }
                            }
                        }
                        else
                        {
                            //Console.WriteLine(" A kerites szin nem egyezik " + str2[i]);

                        }
                    }
                }
                /*                  */

                if (genRand == paros)
                {
                    hszamkert = paratlan;
                    keritesszin = str2[i];
                    Console.WriteLine(index2 + " A kert hazszam " + hszamkert);
                    Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);
                    /*  if (str2[i] == str2[i - 1] || str2[i] == str2[i + 1])
                          {
                          ch = str2[i];
                              foreach (char c in abc)
                                  {
                                  if (c != ch)
                                      {
                                          ;
                                      }
                                  }
                          }*/
                    }
                    if (genRand == paratlan)
                    { //paratlan == odd
                        hszamkert = paratlan;
                        //keritesszin = fence color
                        keritesszin = str2[i];
                        Console.WriteLine(index2 + " A kert hazszam " + hszamkert);//asked home number
                        Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);// the asked home's fence color
                        
                    }
                //.^string testStr = "abc123";
             /*   foreach (char c in abc)
                {
                    //Console.WriteLine(c.ToString());
                    //charVal = Convert.ToChar(str2[i]);
                    charVal = str2[i];
                    if (charVal == str2[i].ToString()) { Console.WriteLine(c.ToString());}
                }*/

            }
            while (!st.EndOfStream);

            Console.WriteLine("- i " + i + "- " + words[0] + "- " + words[1] + "- " + words[2]);
            //All sold site's number
            Console.WriteLine(" Az eladott telkek szama " + i);

            if (Int32.Parse(words[0]) == 0)
                {
                     //last solde site has even number
                     Console.WriteLine(" Az utolso eladott telek paros szamu " + paros);
                }
            else
                {
                //last solde site has odd number
                Console.WriteLine(" Az utolso eladott telek paratlan szamu " + paratlan);
                }
          
            Console.WriteLine(" A kerites szin egyezik az elozovel " + szin + "  hazszam : " + hszam1);
            Console.WriteLine(" A kert hazszam  " + hszamkert);
            if (keritesszin != "#" && keritesszin != ":")
            {
                //keritesszin = fence color
                //fence color
                Console.WriteLine(" A kerites szine " + keritesszin);
            }
            if (keritesszin == "#")
            {
                //fence not painted
                Console.WriteLine("  A keritesnek meg nincs lefestve " + keritesszin);
            }
            if (keritesszin == ":")
            {
                //fence not builded
                Console.WriteLine(" A kerites nem keszult el " + keritesszin);
            }
            //Console.WriteLine(" A kerites szine " + szin);

        }
    }
}
我也想问我在此事中开始新的线程吗?如果是是可以在新线程中移动整个帖子?谢谢,弗兰克。
 

附件

  • kerites.txt.
    764 bytes · Views: 1

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,731
地点
悉尼,澳大利亚
编程经验
10+
我认为这是最好地发布整个代码,在哪里可以看到问题。
不,不是。最好的是编写一个专门分离问题的新代码,并省略对问题无关紧要的一切。
 

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
一般来说,您只会在类中使用阵列进行内部使用。如果要在对象外公开任何数据,那么您应该使用属性,如果要公开数据列表,那么您应该使用集合类型的只读属性。这样,消费者可以获得现有的集合才能获得,设置,添加和删除项目,但它们无法完全替换现有的集合。例如。
C#:
public class Person
{
    public string GivenName { get; set; }
    public string FamilyName { get; set; }
    public List<Person> Children { get; } = new List<Person>();
}
Here, the Person class has two basic properties for the person's given and family names and a read-only collection property that contains other Person objects that represent that person's children. You might use that like this:
C#:
// It's OK to use an array here because it is for internal use.
var people = new[]
             {
                 new Person {GivenName = "Peter", FamilyName = "Smith"},
                 new Person {GivenName = "Paul", FamilyName = "Jones"},
                 new Person {GivenName = "Mary", FamilyName = "Williams"}
             };

var peter = people[0];

peter.Children.AddRange(new[]
                        {
                            new Person {GivenName = "John", FamilyName = "Smith"},
                            new Person {GivenName = "Jane", FamilyName = "Smith"}
                        });

foreach (var person in people)
{
    Console.WriteLine($"{person.GivenName} {person.FamilyName}");

    if (person.Children.Any())
    {
        Console.WriteLine("Children:");

        foreach (var child in person.Children)
        {
            Console.WriteLine($"{child.GivenName} {child.FamilyName}");
        }
    }
    else
    {
        Console.WriteLine("No children");
    }
}
In this case, the collection property in the Person class also contains Person objects, but your types can refer to any other types you like. For instance, the DataTable class has a Columns property that is a collection of DataColumn objects and a Rows property that is a collection of DataRow objects.
谢谢,可爱。
 

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
不,不是。最好的是编写一个专门分离问题的新代码,并省略对问题无关紧要的一切。
谢谢你的帮助。我发布了我的新代码,找到了新的问题。首先,如果我想要一个名列表中的下一个元素到给定元素,则下一步会让我出现问题。索引+1可以覆盖最后一个元素。这是我需要帮助的地方。另一件事,因为我在1到117之间随机问一个数字,看起来我没有总是得到的。请运行代码以查看我尝试描述的内容。谢谢,弗兰克。
我设法修改的代码的一部分:
/*                  */

                if (genRand == paros)
                {
                    hszamkert = paratlan;
                    keritesszin = str2[i];
                    charVal = str2[i].ToLower().ToCharArray()[0];
                    kszinelozo = str2[i-1].ToLower().ToCharArray()[0];
                    //kszinkov = str2[i+1].ToLower().ToCharArray()[0];
                    //if (!st.EndOfStream) { kszinkov = str2[i + 1].ToLower().ToCharArray()[0]; }
                    Console.WriteLine(index2 + " A kert hazszam " + hszamkert);
                    Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);
                    /*  if (str2[i] == str2[i - 1] || str2[i] == str2[i + 1])
                          {
                          ch = str2[i];
                              foreach (char c in abc)
                                  {
                                  if (c != ch)
                                      {
                                          ;
                                      }
                                  }
                          }*/
                    }
                    if (genRand == paratlan)
                    { //paratlan == odd
                        hszamkert = paratlan;
                        //keritesszin = fence color
                        keritesszin = str2[i];
                        charVal = str2[i].ToLower().ToCharArray()[0];
                        kszinelozo = str2[i - 1].ToLower().ToCharArray()[0];
                        //if (!st.EndOfStream) { kszinkov = str2[i + 1].ToLower().ToCharArray()[0]; }
                    Console.WriteLine(index2 + " A kert hazszam " + hszamkert);//asked home number
                        Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);// the asked home's fence color
                        
                    }
            }
            while (!st.EndOfStream);
            // string testStr = "abc123";
            /*  */
            char ch2 = ' ';
            ch2 = charVal;
            Console.WriteLine(" a kerites szin " + charVal);
            Console.WriteLine(" a kerites szin " + kszinelozo);
            Console.WriteLine(" a kerites szin " + kszinkov);
            foreach (char c in abc)
             /*                  */

                if (genRand == paros)
                {
                    hszamkert = paratlan;
                    keritesszin = str2[i];
                    charVal = str2[i].ToLower().ToCharArray()[0];
                    kszinelozo = str2[i-1].ToLower().ToCharArray()[0];
                    //kszinkov = str2[i+1].ToLower().ToCharArray()[0];
                    //if (!st.EndOfStream) { kszinkov = str2[i + 1].ToLower().ToCharArray()[0]; }
                    Console.WriteLine(index2 + " A kert hazszam " + hszamkert);
                    Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);
                    /*  if (str2[i] == str2[i - 1] || str2[i] == str2[i + 1])
                          {
                          ch = str2[i];
                              foreach (char c in abc)
                                  {
                                  if (c != ch)
                                      {
                                          ;
                                      }
                                  }
                          }*/
                    }
                    if (genRand == paratlan)
                    { //paratlan == odd
                        hszamkert = paratlan;
                        //keritesszin = fence color
                        keritesszin = str2[i];
                        charVal = str2[i].ToLower().ToCharArray()[0];
                        kszinelozo = str2[i - 1].ToLower().ToCharArray()[0];
                        //if (!st.EndOfStream) { kszinkov = str2[i + 1].ToLower().ToCharArray()[0]; }
                    Console.WriteLine(index2 + " A kert hazszam " + hszamkert);//asked home number
                        Console.WriteLine(index2 + " A kert hazszam keritesszine " + str2[i]);// the asked home's fence color
                        
                    }
            }
            while (!st.EndOfStream);
            // string testStr = "abc123";
            /*  */
            char ch2 = ' ';
            ch2 = charVal;
            Console.WriteLine(" a kerites szin " + charVal);
            Console.WriteLine(" a kerites szin " + kszinelozo);
            Console.WriteLine(" a kerites szin " + kszinkov);
            foreach (char c in abc)
            {
                //Console.WriteLine(c.ToString());
                //charVal = Convert.ToChar(str2[i]);
                //charVal = str2[i].ToCharArray()[0];
                if (charVal == c)
                { Console.WriteLine(" a kerites szin " + c + charVal); }
            }
 

cfrank2000

活跃的成员
加入
3月6日,2021年
消息
42
编程经验
Beginner
谢谢你的帮助
不幸的是,我对自定义类型有另一个问题。我宣布了
自定义类型的声明:
 Listofrec[] oldal0 = new Listofrec[98];
            
            Listofrec oldal1 = new Listofrec()
            {
                hazszam = 0,
                szin = " "
            };
            oldal0[0] = oldal1;
我试着填满循环
fill up in loop:
                    i2 = i2 + 1;
                    oldal1[i2].szin = str2[i2];
                    oldal1[i2].hazszam = paros;
展示
display:
 for (int d =0; d<98; d++)
            {

                Console.WriteLine(" a kerites szin " + oldal1[d].szin);
                Console.WriteLine(" a kerites szin " + oldal1[d].szin);
              
           }
我试图在网上找到解决方案但没有运气,请帮帮我,谢谢。
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
oldal0 is your array. Your loop above to print things out is trying to access oldal1, a single instance.
 
Last edited:

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
And then once you fix that, assuming that your ListofRec type is still a class (rather than a struct), then you will have an issue where every array element is pointing to the same single instance.
 
最佳 底部