CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Arrays

Arrays
More skills
Assignment operators

Here are listA and listB

listB = listA;

for (int index=0; index<listA.length; index++)
	listB[index] = listA[index];

if (listA == listB)...

  • The expression listA == listB determines if the values of listA and listB are the same and thus determines whether listA and listB refer to the same array
  • To determine whether listA and listB contain the same elements, you need to compare them component by component
  • You can write a method that returns true if two int arrays contain the same elements

boolean areEqualArrays(int[] firstArray,
                      int[] secondArray)
{
    if (firstArray.length != secondArray.length)
        return false;

    for (int index = 0; index < firstArray.length;
                        index++)
        if (firstArray[index] != secondArray[index])
            return false;
    return true;
}

if (areEqualArrays(listA, listB))
...
Previous | Parameters | Pass by value | Assignment operators | Sort | Search | Next