Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 1-Nov-18 6:00am
v4

1 solution

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!
 
Share this answer
 
Comments
Member 13975230 1-Nov-18 11:53am    
Well i just copied bits of my program, i could give you all the code but the named variables are not in english.
Rob Philpott 1-Nov-18 11:55am    
** Pedant Alert **

The ToString() serves no purpose.

Well, the internet is full of annoying people so I'm just embracing it.
OriginalGriff 1-Nov-18 12:08pm    
I'd agree - I just threw it in there so the OP knew it would print it ...
Richard Deeming 2-Nov-18 11:37am    
Actually, it does serve a purpose: it throws a NullReferenceException if any of the elements in the list are null. :)
Member 13975230 1-Nov-18 11:56am    
Basically i have to use 3 classes. One is where i assign all values. Second class has to be a container one which stores temporary values. The third one i have to do all the calculations, like to sum all values and find out whats the avarege...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900