14 Replies Latest reply: Oct 10, 2008 3:28 AM by 799716 RSS

    Array of objects.

    807589
      how can we create an Array of objects of any class?
        • 1. Re: Array of objects.
          799716
          Why not use ArrayList ?
          • 2. Re: Array of objects.
            807589
            can you send me an small example of array list?
            • 3. Re: Array of objects.
              DarrylBurke
              Rajsarawat wrote:
              can you send me an small example of array list?
              phdk already posted a link to the API. Why don't you read that first?

              What's your purpose in wanting to create an array of Object?

              db
              • 4. Re: Array of objects.
                799716
                Rajsarawat wrote:
                can you send me an small example of array list?
                Sure... [an example|http://www.anyexample.com/programming/java/java_arraylist_example.xml]

                But why not read the API first or maybe use google? (Like i did - BTW have not read the example myself just copy-paste from google)
                • 5. Re: Array of objects.
                  807589
                  I want to store records of 10 sudents and the fields are name, phone no, address ... etc.For this I have created a class but i could not create array of objects.so please if it possible please send me a small example to solve this problem.
                  • 6. Re: Array of objects.
                    799716
                    Rajsarawat wrote:
                    I want to store records of 10 sudents and the fields are name, phone no, address ... etc.For this I have created a class but i could not create array of objects.so please if it possible please send me a small example to solve this problem.
                    Okay. Lots of coffee this morning and nothing to do at work!

                    Guess you already have a class called Student.
                    ArrayList<Student> students = new ArrayList<Student>();
                    Now! Please read the API and use google!
                    • 7. Re: Array of objects.
                      DarrylBurke
                      @phdk: Looks like the OP is so new to the language that ArrayList is not the solution here. Also, even the Sun tutorials don't cover this very well... all the array examples are for primitives and String.

                      @OP: To declare an array of a reference type (aka object) of length (say) 10:
                      Student[] students = new Student[10];
                      This creates an array of 10 elements numbered 0 to 9, each of which can accommodate a reference to a Student object. You still have to assign a Student reference to each element of the array. To assign to the 6th element, this would be
                      students[5] = new Student(/*parameters here*/);
                      Note that the index subscript is 5 for the 6th element.

                      db
                      • 8. Re: Array of objects.
                        799716
                        Darryl.Burke wrote:
                        @phdk: Looks like the OP is so new to the language that ArrayList is not the solution here. Also, even the Sun tutorials don't cover this very well... all the array examples are for primitives and String.
                        I guess you're right there.
                        • 9. Re: Array of objects.
                          807589
                          this is not running. please correct this.
                          class student
                          { 
                           int rollno;
                           String name;
                           void getdata(int r,String n)
                           {
                            rollno=r;
                            name=n;
                           }
                           void display()
                          {
                           System.out.println(rollno+"    "+name);
                           }
                          }
                          
                          class usestd
                          {
                           public static void main(String str[])
                          {
                           student[] obj=new student[3];
                            
                           int i;
                           for(i=0;i<3;i++)
                           obj.getdata(111,"ravi");
                          for(i=0;i<3;i++)
                          obj[i].display();

                          }
                          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                          • 10. Re: Array of objects.
                            795426
                            While you get points for using the code tags, you lose them for not giving any error details. It looks like you are confused about the difference between objects and references.

                            When you create an array for a non-primitive type, you are creating an array of references. These references are initialized with the default value for a reference: null. You need to populate your array with new objects before you start calling methods on and accessing members of the array elements.
                            Foo[] array = new Foo[someSize]
                            array[idx] = new Foo();
                            You'll probably want to use a loop of some sort and a variation on the above code.
                            • 11. Re: Array of objects.
                              807589
                              How can we create 40 objects of a class so that we can take records of 40 students. please send me example by correcting my above given example. please...........
                              • 12. Re: Array of objects.
                                807589
                                Rajsarawat wrote:
                                How can we create 40 objects of a class so that we can take records of 40 students. please send me example by correcting my above given example. please...........
                                You don't get it. You ask questions about your code, we answer them, and then you correct your code yourself. We are volunteers, and this isn't a code writing service. Understand?

                                Besides, DeltaGeek has told you what you need to do. I suggest you follow his suggestions. Good luck.

                                Edited by: Encephalopathic on Oct 10, 2008 8:43 PM
                                • 13. Re: Array of objects.
                                  807589
                                  I am anable to do only this following line. how can we create number of objects? please correct this line.
                                  student[] obj=new student[3];
                                  • 14. Re: Array of objects.
                                    807589
                                    Rajsarawat wrote:
                                    student[] obj=new student[3];
                                    As DeltaGeek stated, you have to initialize each item in the array. Each needs to refer to a new Student:
                                        int fubarCount = 5;
                                        MyFubar[] fubars = new MyFubar[fubarCount];
                                        for (int i = 0; i < fubars.length; i++)
                                        {
                                          fubars[i] = new MyFubar();
                                        }