I have this method I'm trying to the return is the student that will be removed and then remove student at pos
I tried almost everything I put the return outside the if statment , but I still get error I also tried to substract 1 from the size but I still get error. the error is arrayIndexOutOfBound it said index 4 and size 4 .. what should I do

    public Student removeStudentAt(int index) {
            Student stu = list.get(index);
            if (index < 0 || index > list.size) {
                return null;
            } else {
                list.remove(index);
                return stu;
            }
        }

my next problem is
I have class school extend students and the class that I'm writing the code in is 'AllSchool' it have arrayList of type school and I'm trying to create string of array to with the full name of all the students and return the array of the student(full names)
students class have these attribute
private String fName;
private String sName;
private int id;
I have get and set method for the attribute and getfullName method

I'm not sure what I did wrong ,but here is what I did
The error that I got is array length differed expected length 7 actuak kength 8

         public String[] getAllNames() {


        String[] allNames = new String[size()];
        int i = allNames.length;
        int n = ++i;
        String[] copyArray = new String[n];
        for (int j = 0; j < allNames.length; j++) {
            copyArray[j] = allNames[j];

        }
        return copyArray;
    }

how can I fix my code .. did I miss something ?

For problem 1:

It could be that index 4 actually means that the max size is 5. Seeing as an index can start at 0.

This can happen in line 2 where it looks for the index which isn't there.

Not excatly sure how you can get an error in problem 2? Do you get an error at line 9?

First problem is that you try to get(index) on line 2 before you validate that index on line 3. If the array size is 4 then valid indexes are 0-3. That's just a simple programming error.

As for the second problem,I can't get past the first line - it's a nonsense for School to extend Student - a school is not a kind of student. Then what the class AllSchool about?

Anyway, ignoring that, the method code makes even less sense:
You create a new String array (uninitialised - all entries are null)
You then create a second array that's one element bigger
You then copy the nulls from the first array to the second array (leaving its last element unchanged (still null)).
Finally you return the larger array full of nulls. I'm baffled.

There's a whole load of confusion here. Probably what you need is a School class that has a collection of Students as an instance member. Then you get all the sudent names by creating a string array same size as the Sudent collection, then iterate through all the Sudents getting their names and copying them to the string array.

Thank you I managed to fix the first method here is how I fixed it

 public Student removeStudentAt(int index) {
Student stu;
if (index < 0 || index > list.size-1) {
return null;
} else {
 stu = list.get(index);

list.remove(index);
return stu;
}
}

I'm not sure what is wrong with the second method I don't get any red lines

the error is array length differed expected length 7 actual length 8

actually

size

sure how

For problem 1:

It could be that index 4 actually means that the max size is 5. Seeing as an index can start at 0.

This can happen in line 2 where it looks for the index which isn't there.

Not excatly sure how you can get an error in problem 2? Do you get an error at line 9?

I just spent 10 minutes explaining exactly what you did in your second method. Basically it's valid syntax (hence no compiler error lines), but logically it makes no sense at all. I also explained how it should work. I guess you didn't bother to read all that.

Thank you for your help I managed to solve it

First problem is that you try to get(index) on line 2 before you validate that index on line 3. If the array size is 4 then valid indexes are 0-3. That's just a simple programming error.

As for the second problem,I can't get past the first line - it's a nonsense for School to extend Student - a school is not a kind of student. Then what the class AllSchool about?

Anyway, ignoring that, the method code makes even less sense:
You create a new String array (uninitialised - all entries are null)
You then create a second array that's one element bigger
You then copy the nulls from the first array to the second array (leaving its last element unchanged (still null)).
Finally you return the larger array full of nulls. I'm baffled.

There's a whole load of confusion here. Probably what you need is a School class that has a collection of Students as an instance member. Then you get all the sudent names by creating a string array same size as the Sudent collection, then iterate through all the Sudents getting their names and copying them to the string array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.