我如何选择arraylist? [英] How do I take the arraylist?

查看:45
本文介绍了我如何选择arraylist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一个问题,我不知道如何从一个班级中取出一个arraylist并将其打印出来。

所以我有一个文本文件;

 Informatikos fakultetas 
Petraitis;乔纳斯; IF-1/8; 3; 10 8 9 0
Algaitis;阿尔吉斯; IF-1/8; 3; 10 0 9 9
Petraitis; Kazys; IF-1/8; 4; 9 9 10 10
Algaitis; RIMAS; IF-1/8; 2; 9 9 0 0
Petraitis; Vytas; IF-1/9; 4; 8 9 7 5
Petraitis; Anupras; IF-1/9; 3; 8 7 6 0
Petraitis;维达斯; IF-1/9; 2; 9 8 0 0
Petraitis; Anzelmas; IF-1/9; 3; 9 9 9 0
Petraitis;雅斯科维; IF-1/9; 4; 9 10 9 7







现在我将方法中的arraylist分配给了一个类,它是太多了解。现在我需要在类Program中创建一个方法,在其中我可以使用FOREACH获取arraylist的值,而不是linq。问题是我不知道如何。我的老师说你必须在forcycle中用一个for循环来调用它,但我不知道如何。即使我这样打印了这个:

 System.Collections.ArrayList 
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
按任意键继续。 。 。





所以我需要知道如何将一个类的arraylist值调用到另一个类。同时我不能在main中使用任何for或foreach循环,我只能在课程中使用它们编程mehthods。

确定完整程序但不是英语:

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Collections;
使用System.IO;

namespace ConsoleApp2
{
class Studentas
{
privatestringpavardė,vardas,grupė;
private int kieks;
private ArrayList paz;
private double vidurkis;

public Studentas()
{$ b $bpavardė=;
vardas =; $ b $bgrupė=;
kieks = 0;
vidurkis = 0;
paz = new ArrayList();
}

publicvoidDėti(string pav,string vard,string grup,int kiek,ArrayList pz)
{$ b $bpavardė= pav;
vardas = vard; $ b $bgrupė= grup;
kieks = kiek;
foreach(int sk in pz)
paz.Add(sk);
}
publicvoidDėti2(string pav,string vard,string grup,int kiek,double vid)
{$ b $bpavardė= pav;
vardas = vard; $ b $bgrupė= grup;
kieks = kiek;
vidurkis = vid;
}

public ArrayList ImtiArray(){return paz; }

公共覆盖字符串ToString()
{
string eilute;
eilute = string.Format({0,-12} {1,-9} {2,-7} {3,1:d},$ b $bpavardė,vardas,grupė,kieks) ;
foreach(int sk in paz)
eilute = eilute + string.Format({0,3:d},sk);
返回eilute;
}

公共字符串Tostr()
{
string eilute;
eilute = string.Format({0,-12} {1,-9} {2,-7} {3,2:F},$ b $bpavardė,vardas,grupė,PazVid( ));
返回eilute;
}

public double PazVid()
{
double sum = 0;
foreach(在巴黎举行)
{
sum = sum + sk;
}
返还金额/ kieks;
}

}
类Fakultetas
{
const int CMax = 100;
私人Studentas [] St;
private int n;

public Fakultetas()
{
n = 0;
St = new Studentas [CMax];
}

public int Imti(){return n; }

public Studentas Imti(int i){return St [i]; }

publicvoidDėti(Studentas ob){St [n ++] = ob; }

}
class程序
{
const string CFd =.. \\..\\\。
const string CFr =.. \\..\\Rezultatai.txt;

static void Main(string [] args)
{
Fakultetas grupes = new Fakultetas();
Fakultetas grupes1 =新的Fakultetas();
string pav;
ArrayList pz = new ArrayList();

if(File.Exists(CFr))
File.Delete(CFr);
Skaityti(ref grupes,CFd,out pav);
Spausdinti(grupes,CFr,Pradinisstudentsųsąrašas,pav,0);
Formuoti(grupes,ref grupes1);


if(grupes1.Imti()> 0)
{
Spausdinti(grupes1,CFr,Naujasstudentsųsąrašas,pav,1);
}
FVid(grupes1,pz);
}
static void FVid(Fakultetas grupes1,ArrayList pz)
{
foreach(int sk in pz)
{
Console.WriteLine(grupes1。 IMTI(SK).ImtiArray(PZ));
}
}

static void Skaityti(ref Fakultetas grupe,string fv,out string pav)
{
using(StreamReader reader = new StreamReader( fv))
{
string pv,vrd,grp;
int kiek;
ArrayList pz = new ArrayList();
使用(StreamReader fr = new StreamReader(fv))
{
string pavr;
pavr = fr.ReadLine();
pav = pavr;
}
string [] lines = File.ReadAllLines(fv,Encoding.GetEncoding(1257));
int n = lines.Length;
for(int i = 1; i< = n-1; i ++)
{
string [] parts = lines [i] .Split(';');
pv = parts [0] .Trim();
vrd = parts [1] .Trim();
grp = parts [2] .Trim();
kiek = int.Parse(parts [3] .Trim());
// paz
string [] eil = parts [4] .Trim()。Split(new [] {''},
StringSplitOptions.RemoveEmptyEntries);
pz.Clear();
foreach(字符串eilute in eil)
{
int aa = int.Parse(eilute);
pz.Add(aa);
}
Studentas stud = new Studentas(); $ b $bstud.Dėti(pv,vrd,grp,kiek,pz); $ b $bgrupe.Dėti(stud);
}
}
}

static void Spausdinti(Fakultetas grupe,string fv,stringantraštė,string pav,int n)
{
使用(var fr = File.AppendText(fv))
{
fr.WriteLine(pav);
fr.WriteLine(----------------------------------------- - );
if(n == 1)
{
string virsus =
---------------------- -------------------- \\ n
+PavardėVardasGrupėVidurkis\\ n
+ ------------------------------------------;
fr.WriteLine(antraštė);
fr.WriteLine(virsus);
}
其他
{
string virsus =
----------------------- ------------------- \\ n
+PavardėVardasGrupėPažymiai\\ n
+ - -----------------------------------------;
fr.WriteLine(antraštė);
fr.WriteLine(virsus);
}
for(int i = 0; i< grupe.Imti(); i ++)
{
if(n == 0)
{
fr.WriteLine({0},grupe.Imti(i).ToString());
}
else
{
fr.WriteLine({0},grupe.Imti(i).Tostr());
}
}
fr.WriteLine(------------------------------- ----------- \r\\\
);
}
}
static void Formuoti(Fakultetas grupes,ref Fakultetas grupes1)
{
for(int i = 0; i< grupes.Imti(); i ++)
{$ b $bgrupes1.Dėti(grupes.Imti(i));
}
}
}
}





我尝试了什么:



现在一切都很无望真的需要帮助

解决方案

首先, ArrayList是 Student 类的一部分,这看起来有点奇怪 - 你有一个插入值的方法,但是你没有调用它,我不知道为什么它就在那里。



您还可以在 Main 方法中创建一个方法,这似乎也无关紧要因为你没有显然对那个做任何事情。



我只是猜测你的学校需要一个收藏品学生对象 - 它的名称为 St - 但你没有说明你是如何得到任何东西的那两个!



我认为您需要做的就是停在那里,将解决方案文件夹复制到安全的地方,然后重新开始。仔细阅读问题 - 我认为你并没有完全按照说明正确地进行操作,需要重新考虑它。



但是打印一个ArrayList不仅仅是一个Console.WriteLIne(myArrayList)的情况 - 您需要使用循环迭代集合的每个元素:

 foreach(MyArrayList中的对象obj)
{
Console.Write(obj.ToString());
}
Console.WriteLine();

如果您在整个ArrayList上调用ToString,您将获得完整的类名System.Collections.ArrayList - 就像您拥有的那样看到!


So i got a problem, i dont know how to take an arraylist from one class and print it out in main.
So i got a text file;

Informatikos fakultetas
Petraitis; Jonas; IF-1/8; 3; 10 8 9 0
Algaitis; Algis; IF-1/8; 3; 10 0 9 9
Petraitis; Kazys; IF-1/8; 4; 9 9 10 10
Algaitis; Rimas; IF-1/8; 2; 9 9 0 0
Petraitis; Vytas; IF-1/9; 4; 8 9 7 5
Petraitis; Anupras; IF-1/9; 3; 8 7 6 0
Petraitis; Vidas; IF-1/9; 2; 9 8 0 0
Petraitis; Anzelmas; IF-1/9; 3; 9 9 9 0
Petraitis; Šarūnas; IF-1/9; 4; 9 10 9 7




Now i assigned the arraylist in a method to a class, where it is too much to understand. Now i need to create a method in class Program in which i can get the values of arraylist with FOREACH, not with linq. The problem is i dont know how. My teacher said you have to call it out with a for cycle inside a forcycle but i dont know how. Even if i did it printed out this:

System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
System.Collections.ArrayList
Press any key to continue . . .



So i need to know how do i call out the arraylist values from one class to another. Meanwhile i cannot use any for or foreach cycles in main, i can only use them in class Program mehthods.
Ok sp heres the full program but not in english:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace ConsoleApp2
{
    class Studentas
    {
        private string pavardė, vardas, grupė;
        private int kieks;
        private ArrayList paz;
        private double vidurkis;

        public Studentas()
        {
            pavardė = "";
            vardas = "";
            grupė = "";
            kieks = 0;
            vidurkis = 0;
            paz = new ArrayList();
        }

        public void Dėti(string pav, string vard, string grup,int kiek, ArrayList pz)
        {
            pavardė = pav;
            vardas = vard;
            grupė = grup;
            kieks = kiek;
            foreach (int sk in pz)
                paz.Add(sk);
        }
        public void Dėti2(string pav, string vard, string grup, int kiek,double vid)
        {
            pavardė = pav;
            vardas = vard;
            grupė = grup;
            kieks = kiek;
            vidurkis = vid;
        }

        public ArrayList ImtiArray() { return paz; }

        public override string ToString()
        {
            string eilute;
            eilute = string.Format("{0, -12} {1, -9} {2, -7} {3,1:d}",
            pavardė, vardas, grupė,kieks);
            foreach (int sk in paz)
                eilute = eilute + string.Format("{0, 3:d}", sk);
            return eilute;
        }

        public string Tostr()
        {
            string eilute;
            eilute = string.Format("{0, -12} {1, -9} {2, -7} {3,2:F}",
            pavardė, vardas, grupė, PazVid());
            return eilute;
        }

        public double PazVid()
        {
            double sum = 0;
            foreach(int sk in paz)
            {
                sum = sum + sk;
            }
            return sum / kieks;
        }

    }
    class Fakultetas
    {
        const int CMax = 100; 
        private Studentas[] St;
        private int n;

        public Fakultetas()
        {
            n = 0;
            St = new Studentas[CMax];
        }

        public int Imti() { return n; }

        public Studentas Imti(int i) { return St[i]; }

        public void Dėti(Studentas ob) { St[n++] = ob; }

    }
    class Program
    {
        const string CFd = "..\\..\\A.txt";
        const string CFr = "..\\..\\Rezultatai.txt";

        static void Main(string[] args)
        {
            Fakultetas grupes = new Fakultetas();
            Fakultetas grupes1 = new Fakultetas();
            string pav;
            ArrayList pz = new ArrayList();

            if (File.Exists(CFr))
                File.Delete(CFr);
            Skaityti(ref grupes, CFd, out pav);
            Spausdinti(grupes, CFr, " Pradinis studentų sąrašas", pav,0);
            Formuoti(grupes, ref grupes1);


            if (grupes1.Imti() > 0)
            {
                Spausdinti(grupes1, CFr, " Naujas studentų sąrašas", pav, 1);
            }
            FVid(grupes1,pz);
        }
        static void FVid(Fakultetas grupes1,ArrayList pz)
        {
            foreach(int sk in pz)
            {
                Console.WriteLine(grupes1.Imti(sk).ImtiArray(pz));
            }
        }

        static void Skaityti(ref Fakultetas grupe, string fv, out string pav)
        {
            using (StreamReader reader = new StreamReader(fv))
            {
                string pv, vrd, grp;
                int kiek;
                ArrayList pz = new ArrayList();
                using(StreamReader fr = new StreamReader(fv))
                {
                    string pavr;
                    pavr = fr.ReadLine();
                    pav = pavr;
                }
                string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
                int n = lines.Length;
                for (int i= 1;i <=n-1;i++)
                {
                    string[] parts = lines[i].Split(';');
                    pv = parts[0].Trim();
                    vrd = parts[1].Trim();
                    grp = parts[2].Trim();
                    kiek = int.Parse(parts[3].Trim());
                    //paz
                    string[] eil = parts[4].Trim().Split(new[] { ' ' },
                    StringSplitOptions.RemoveEmptyEntries);
                    pz.Clear();
                    foreach (string eilute in eil)
                    {
                        int aa = int.Parse(eilute);
                        pz.Add(aa);
                    }
                    Studentas stud = new Studentas();
                    stud.Dėti(pv, vrd, grp, kiek, pz);
                    grupe.Dėti(stud);
                }
            }
        }

        static void Spausdinti(Fakultetas grupe, string fv, string antraštė, string pav, int n)
        {
            using (var fr = File.AppendText(fv))
            {
                fr.WriteLine(pav);
                fr.WriteLine("------------------------------------------");
                if(n == 1)
                {
                    string virsus =
            "------------------------------------------\r\n"
            + " Pavardė     Vardas     Grupė   Vidurkis \r\n"
            + "------------------------------------------";
                    fr.WriteLine(antraštė);
                    fr.WriteLine(virsus);
                }
                else
                {
                    string virsus =
            "------------------------------------------\r\n"
            + " Pavardė     Vardas     Grupė   Pažymiai \r\n"
            + "------------------------------------------";
                    fr.WriteLine(antraštė);
                    fr.WriteLine(virsus);
                }
                for (int i = 0; i < grupe.Imti(); i++)
                {
                    if (n == 0)
                    {
                        fr.WriteLine("{0}", grupe.Imti(i).ToString());
                    }
                    else
                    {
                        fr.WriteLine("{0}", grupe.Imti(i).Tostr());
                    }
                }
                fr.WriteLine("------------------------------------------\r\n");
            }
        }
        static void Formuoti(Fakultetas grupes,  ref Fakultetas grupes1)
        {
            for (int i = 0; i < grupes.Imti(); i++)
            {
                    grupes1.Dėti(grupes.Imti(i));
            }
        }
    }
}



What I have tried:

Everything by now im hopeless really need help

解决方案

First off, the ArrayList is part of the Student class, which seems a little odd - you have a method to insert values, but you don't call it, and I'm not sure why it is there at all.

You also create one in your Main method which also seems irrelevant because you don't obviously do anything with that one either.

I'm just guessing that your School needs a collections of Student objects - which it has under the name of St - but you don't show how you get anything into that either!

I think what you need to do is stop there, copy the solution folder to a safe place, and then start again. Read the question carefully - I don't think you are quite following the instructions at all correctly and need to rethink it.

But printing an ArrayList isn't just a case of Console.WriteLIne(myArrayList) - you need to iterate each of the elements of the collection using a loop:

foreach (object obj in MyArrayList)
   {
   Console.Write(obj.ToString());
   }
Console.WriteLine();

IOf you call ToString on the whole ArrayList all you will get is the full class name "System.Collections.ArrayList" - as you have seen!


这篇关于我如何选择arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆