11 Replies Latest reply: Dec 26, 2007 4:51 PM by 807603 RSS

    How to copy an array element in one class to an array in another class?

    807603
      Hi,

      I have a ClassRoom class that stores a list of Student objects in an array. How would I copy a Student object from the Student[] array in the ClassRoom class to an array in another class?

      Is it something like this:
      System.arraycopy(Students, 2, AnotherClass.Array, 0, 2);
      In an array do the items get copied over existing array elements or can the be added to the end? If so, how would I specify add copied object reference to the end of the array in the other class?
        • 1. Re: How to copy an array element in one class to an array in another class?
          807603
          The best way to answer this question is twofold:

          1) Read the API:
          http://java.sun.com/javase/6/docs/api/java/lang/System.html#arraycopy

          and
          2) Try it yourself. It would be trivial to write a small app to test just your quesitons.
          • 2. Re: How to copy an array element in one class to an array in another class?
            807603
            You don't need to copy the objects from one class to another, this is what access modifiers are used for. First of all it would be better to use a Vector for this anyway since it sounds like multiple objects may be accessing the data for the students simultaneously. So what you would want to do is create a public vector, pass in the information about each student as a parameter

            i.e.
            public static Vector<Student> student = new Vector<Student>();
            //other declarations
            Student s1 = new Student("name", "age", "grade");
            //more students
            student.add(s1);
            Hope that helps.
            • 3. Re: How to copy an array element in one class to an array in another class?
              795426
              drew22299 wrote:
              Hi,

              I have a ClassRoom class that stores a list of Student objects in an array. How would I copy a Student object from the Student[] array in the ClassRoom class to an array in another class?

              Is it something like this:
              System.arraycopy(Students, 2, AnotherClass.Array, 0, 2);
              In an array do the items get copied over existing array elements or can the be added to the end? If so, how would I specify add copied object reference to the end of the array in the other class?
              System.arrayCopy will overwrite whatever is already in the array. It is your job to make sure it copies into the proper array location.

              That being said, you're only moving a single student. This is not something you would use arrayCopy for, as you can just do that with simple assignment. Also, you should consider giving Class a method to add a student to its student list, as the class should know how many students it has and can easily "append" to the array.

              Note: I hope you noticed the quotes around append. Java's arrays are fixed size. Once allocated, their size cannot change. You may want to consider using one of the List implementations (ArrayList, for example) instead.
              • 4. Re: How to copy an array element in one class to an array in another class?
                807603
                So to append a Student object from the Student[] array to a different array I would do something like:
                students[1].append(differentArray.length + 1);
                I have tried looking for the correct syntax but still don't understand how to do this.
                • 5. Re: How to copy an array element in one class to an array in another class?
                  807603
                  You cannot modify the length of an array once it has been initialized. Objects like Vectors and ArrayLists can be adjusted, and furthermore Vectors are thread safe.

                  Check out the API: http://java.sun.com/j2se/1.5.0/docs/api/ and look under Vector
                  • 6. Re: How to copy an array element in one class to an array in another class?
                    807603
                    dnbphysicist wrote:
                    You cannot modify the length of an array once it has been initialized. Objects like Vectors and ArrayLists can be adjusted, and furthermore Vectors are thread safe.
                    I don't want to open a can of worms here, but if you want a synchronized version of ArrayList, I'd recommend against use of Vector and instead take the ArrayList and give it a synchronized wrapper with a call to Collections.synchronizedList( ).
                    • 7. Re: How to copy an array element in one class to an array in another class?
                      795426
                      Somehow I doubt that thread safety is an issue at this point and ArrayList will work just fine.

                      I'd also like to open that can of worms a little wider and point out the classes in java.util.concurrent that were added in java 1.5 to supplement the Collections.synchronizedFoo methods.
                      • 8. Re: How to copy an array element in one class to an array in another class?
                        807603
                        DeltaGeek wrote:Somehow I doubt that thread safety is an issue at this point and ArrayList will work just fine.
                        You're probably right, I just thought I would throw it out there for the OP :) Nonetheless he is going to have to use something from the Collections framework without knowing a priori how many Student objects he is going to have.
                        • 9. Re: How to copy an array element in one class to an array in another class?
                          807603
                          Can someone please provide me with example code for the following:

                          >
                          That being said, you're only moving a single student. This is not something you would use arrayCopy for, as you can just do that with simple assignment. Also, you should consider giving Class a method to add a student to its student list, as the class should know how many students it has and can easily "append" to the array.

                          Note: I hope you noticed the quotes around append. Java's arrays are fixed size. Once allocated, their size cannot change. You may want to consider using one of the List implementations (ArrayList, for example) instead.>

                          Edited by: drew22299 on Dec 26, 2007 12:10 PM
                          • 10. Re: How to copy an array element in one class to an array in another class?
                            795426
                            drew22299 wrote:
                            Can someone please provide me with example code
                            class Clazz{
                              // Need to store the *List* of students somehow
                            
                              public void addStudent(Student){
                                // Add the student to my internal list. I'm sure there's a method for that somewhere in the javadocs...
                              }
                            
                              public void removeStudent(Student){
                                // Remove the student from my internal list. I wonder if there's a method to do that as well...
                              }
                            }
                            • 11. Re: How to copy an array element in one class to an array in another class?
                              807603
                              Why doesn't create an access method
                              public Student[] getArrayOfStudents()
                              ?