בעיה ב ArrayList

בעיה ב ArrayList

NameCustomerWithMovies[j].Add(m);

NameCustomerWithMovies= שם המערך jaggarry m= מחלקה אני מנסה להוסיף מחלקה לתוך המערך הזה ולא מצליח מופיעה לי שגיאת חריגה :( מה אפשר לעשות
 

yuval k

New member
מה השגיאה?

ומה אתה מנסה לעשות ע"י ה-[j], אם המערך שלך הוא NameCustomerWithMovies עצמו?
 

yuval k

New member
אבל לא ככה פועל ArrayList

ב-ArrayList מאוחסנים הפריטים בתור מערך חד מימדי. כשאתה מוסיף, נוסף מיקום אוטומטית ברשימה.
 

pagis

New member
הכוונה שלך לא ברורה

אתה אומר ARRAYLIST, כותב במבנה של JAG ואז אומר JAG. אני לא מבין מה בדיוק אתה רוצה. תראה קוד מקור
 
פרוט לכוונה שלי

אי מנסה להכניס מחלקה של לקוח ל מערך של arraylist בזה אין בעיה אני יודע מה לעשות אבל כשאני מנסה להכניס מחלקה ל jagg זה בכלל לא עובד... הרי jagg הוא דו ממדי לעומת הarray הרגיל וניסיתי להכניס לשורה 1 עמודה 1 את המחלקה קפיש? בקיצור להכניס את ה מחלקה של לקוח למערך jagg
 

pagis

New member
עבודה עם מערך JAGGED ומחלקות

טוב, יש לי קצת זמן פנוי בזמן שה DB עושה גיבוי אז נעשה את זה מסודר: מערך מסוג JAGGED הוא בעצם מערך של מערכים חד ממדיים מאותו סוג (כלומר כל המערכים החד ממדיים הם מאותו סוג וההכרזה על ה JAGGED היא גם מהסוג הזה). אתה מאתחל אותו כסוג המחלקה שאתה רוצה לעבוד איתה ואז אתה צריך גם לאתחל (לא להכריז!!!) כל אחד מהמערכים החד ממדיים. הנה דוגמת קוד פשוטה:
namespace ConsoleApplication1 { class Person { public string firstName; public string lastName; public byte[] id; public Person(string FirstName,string LastName,byte[] ID) { this.firstName = FirstName; this.lastName = LastName; this.id = ID; } } class App { public static void Main() { Person[][] jag = new Person[10][]; for (int i=0 ; i<jag.Length ; ++i) jag = new Person[10]; jag[5][3] = new Person("Dan","Pagis",new byte[]{7,7,7,7,7,7,2}); Console.WriteLine(jag[5][3].firstName); } } }

אם יש עוד שאלות תרגיש חופשי אבל אני זז הביתה אז תשובה תקבל (ממני) רק מחר.
 

pagis

New member
אתה מנסה לגשת לאובייקט שלא אותחל

אם תראה לי את הקוד אגיד לך איפה הבעיה. בנתיים ניחוש מושכל יהיה שאתה מנסה למקם אובייקט בתוך תת מערך שלא אותחל עדיין או לחילופין שאתה מנסה לגשת לאינדקס במערך שלא אותחל/לא הוצב בו אובייקט.
 

pagis

New member
אתה מנסה לגשת לאובייקט שלא אותחל

אם תראה לי את הקוד אגיד לך איפה הבעיה. בנתיים ניחוש מושכל יהיה שאתה מנסה למקם אובייקט בתוך תת מערך שלא אותחל עדיין או לחילופין שאתה מנסה לגשת לאינדקס במערך שלא אותחל/לא הוצב בו אובייקט.
 
הקוד:

public ArrayList [][]CustomerWithMovies; public RentMovies(ref ManagerMovies mm,ref ManagerCostumers mc) { // This call is required by the Windows Form Designer. InitializeComponent(); this.mm=mm; this.mc=mc; Ac=new Costomers();//=new Costomers(); Am=new Movies();//=new Movies(); CustomerWithMovies=new ArrayList[mc.MyArryListCostumers.Count][]; // TODO: Add any initialization after the InitializeComponent call } private void BtnCheckiFCustmerInList_Click(object sender, System.EventArgs e) { int i=0,j=1; foreach(Custmer c in mc.MyArryListCostumers) { //אם השם קיים if(c.LastNamePerson==txtLName.Text) { CustomerWithMovies[0].Add(c.LastNamePerson); while(j<listBoxAddNameMoviesToCustomer.Items.Count) { string Name=(string)(listBoxNameCustomerListMovies.Items[j]); foreach(Movie m in mm.MyArryListmovies) { if(m.MyMovieName==Name) { CustomerWithMovies[j].Add(m); break; } } j++; } j=1; i++; break; } } } מקווה שתבין..
 
ננתי לך בטעות קוד לא מוכן

ublic ArrayList [][]CustomerWithMovies;
ublic ArrayList [][]CustomerWithMovies; CustomerWithMovies=new ArrayList[3][]; // CustomerWithMovies=new ArrayList[mc.MyArryListCostumers.Count][]; for(int i=0;i<mc.MyArryListCostumers.Count;i++) { CustomerWithMovies=new ArrayList[3]; } CustomerWithMovies[2][2].Add("plz");

CustomerWithMovies[2][2].Add("plz"); השורה הזו גורמת לי בעיות והשיגאיה שניתנת לי היא: An unhandled exception of type 'System.NullReferenceException' occurred in DvD.exe Additional information: Object reference not set to an instance of an object.
 

pagis

New member
ממבט קצר

אני רואה שאתה לא מאתחל את מערכי המשנה. מה שעשית כאן הוא ליצור מערך JAGGED שמכיל מערכי משנה מסוג ARRAYLIST. אתה צריך לעבור על כל אחד ממערכי המשנה האלה ולאתחל אותם, תתיחס לכל אחד מהם כאילו מדובר במערך חד ממדי של אובייקטים מסוג ARRAYLIST. כלומר:
int clients = 10; // The number of 1 dimensional arrays of type ArrayList in the jagged array. int maxArrayListsPerClient = 100; ArrayList[][] jag = new ArrayList[num][]; // a jagged array of type ArrayList is declared and started. for (int i=0 ; i<jag.Length ; ++i) jag = new ArrayList[maxArrayListsPerClient];

הקוד הזה מראה למה הכוונה באיתחול מערכי המשנה. בכל מקרה, לא בדיוק הבנתי למה ליצור מערך JAGGED של אובייקטים מסוג ARRAYLIST. אני לא יודע אם אתה באמת צריך את זה אבל בטוח יש דרך טובה יותר לעשות את מה שאתה רוצה. תוכנית כזו לא יכולה לעבוד בקנה מידה גדול בלי לאכל לך כל חלק פנוי מהזכרון.
 
איך אפשר להכניס ערך למערך?

ניסיתי לעשות
int clients = 10; // The number of 1 dimensional arrays of type ArrayList in the jagged array. ArrayList[][] jag = new ArrayList[clients][]; // a jagged array of type ArrayList is declared and started. for (int i=0 ; i<jag.Length ; ++i) jag = new ArrayList[clients]; jag[0][1].Add("dg");;

עדין עושה בעיות אני הולך להתאבד....
 

pagis

New member
לא איתחלת את האובייקט ARRAYLIST

צריך, אחרי שאיתחלת את כל המערכים לאתחל אובייקט אובייקט:
int num = 10; int maxArrayListsPerClient = 100; ArrayList[][] jag = new ArrayList[num][]; // a jagged array of type ArrayList is declared and started. for (int i=0 ; i<jag.Length ; ++i) jag = new ArrayList[maxArrayListsPerClient]; for (int i=0 ; i<jag.Length ; ++i) for (int j=0 ; j<jag.Length ; ++j) jag[j] = new ArrayList(); jag[0][1].Add("bla bla"); Console.WriteLine((string)(jag[0][1][0]));
 
למעלה