简体   繁体   中英

In Java, given an array of object references, how do I check that all the references in the array are the same?

We just started learning references in Java and this one stumped me a bit. Any help is appreciated!

You iterate the array and use == to compare the first array member against all others.

If there is any mismatch, you can stop, knowing that at least one entry is not the same.

Equality operator will work to check if they have same reference and point to same object. eg:

array[0]==array[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM