aspose file tools*
The moose likes Beginning Java and the fly likes working with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working with arrays" Watch "working with arrays" New topic
Author

working with arrays

Ralph Reinbeck
Greenhorn

Joined: Jul 22, 2006
Posts: 3
Hi there. I'm trying to do a few simple things with arrays of integers, but I'm having a bit of trouble. First, I'd like to write some code which returns true if two adjacent array elements have the same value, and it returns false otherwise. So as soon as it finds two matching entries (if they exist), it should return true. Here's what I tried (the array and the duplicates variable have been declared) -- these types of for loops always give me trouble, because I don't necessarily want to run the test for EVERY pair of entries, but rather pairs that don't match:

=====
for (int k = 0; k < array.length; k++)
if (array[k] = array[k + 1]) {
duplicates = true;
else
duplicates = false;
}
=====

I can see right away that this is going to be trouble, because when k = array.length - 1, there is no entry a[k + 1]. But I'm not sure what else to try.

My second problem is similar -- I'd like to see if ANY two elements of the array have the same value. I think I'll need two indices here, so this is what I've tried so far:

=====
for (j = 0; j < array.length; j++) {
for (k = 0; k < array.length; k++) {
if (array[0] == array[j])
duplicates = true;
else
duplicates = false;
}
}
=====

Again, I realize that I don't necessarily need to cycle through the entire range of values for my indices; I could get lucky and be able to stop as soon as the first pair of values. Any suggestions for how to avoid that mistake in logic?

I really appreciate any help that's out there.

Thanks!

R
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The main problem here is that the duplicates flag is set after every comparison; therefore, the final value will be the value of the very last comparison done. Even if there are duplicates, this loop will only report if the last two elements are duplicated.

So what you need to do is terminate the loop early. There are two ways to do this: use the "break" keyword, or use "return" if this loop is the whole body of a method. There are people who prefer both styles. My personal preference is to eliminate the duplicates flag altogether, and use "return", putting this loop in its own method.


[Jess in Action][AskingGoodQuestions]
Ralph Reinbeck
Greenhorn

Joined: Jul 22, 2006
Posts: 3
Hi Ernest,

Thanks for your reply! I still have a question, though -- I understand that my approach sets the boolean flag after every comparison, but I don't understand how to use the "return" as you suggested.

Thanks!

R
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
EFH: There are two ways to do this: use the "break" keyword, or use "return" if this loop is the whole body of a method...

You can also use your flag as a condition in your for loop.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
Use return like this (in psuedocode):

if(a == b) then return true;
else return false;

translating it to code:



So your loop would do something like:

I can see right away that this is going to be trouble, because when k = array.length - 1, there is no entry a[k + 1]. But I'm not sure what else to try.


Believe it or not, you actually answered your own question. If k = array.length, there indeed will not be a a[k + 1]. But what if you stop your loop one iteration before k = array.length?
[ July 23, 2006: Message edited by: Chad Clites ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

is better written as:

which in turn is better written by deleting the method altogether.


Tony Morris
Java Q&A (FAQ, Trivia)
Ralph Reinbeck
Greenhorn

Joined: Jul 22, 2006
Posts: 3
Thanks everyone -- your suggestions were very helpful. I appreciate them!

R
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with arrays
 
Similar Threads
finding intersection of two arrays, remove duplicates w/out error message
sorting multidimensional arrays (int)
Sun Cirtification
Finding Nth Largest element of an array without sorting
Check an array for equality