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());
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