I need a little help. Here are the specs:

Create a method called findLargest, which takes an array of integers as an input parameter and returns the array index of the largest integer in the array. If there are positions that are tied for largest, the method should return the first index with a largest value.
Examples:
data:{25,11,15,25}
==> 0
data:{2,11,15,25}
==> 3

Here is the line of code given:

``public int findLargest(int[] data){ }``

This is what I have so far.

``````int index = 0;
int largest;
largest = data[0];
for(int i =0; i < index; i++)
if(largest < data[i])
{
largest = data[i];
return data[i];
}``````

Not sure where to go from here.

## All 7 Replies

Hint: Is index correct?

``````int i;
int index = 0;
int largest;
largest = data[0];
for(i =0; i < index++; i++)
if(largest < data[i])
{
largest = data[i];
}
return index;``````

This is what I came up with but it is not producing the correct code. Its not looping correctly. It returns the index of the second array if it is larger than the first.

``````int i;
int index = 0;
int largest;
largest = data[0];
for(i =0; i < data.lenght(); i++)
if(largest < data[i])
{
largest = data[i];
index = i;
}
return index;``````

Thanks It worked but I had to take the () out. What is the difference in using the data.lentgth and the index. I not quite sure what the .lenght references.

When do I finaly learn where to use this bracklets ;) ?
data.length will return size of your array so you do not have to exactly say loop only 4 times then stop, you just ask for size of array and loop till you get last element in array

your index is just variable which you declared and then you tried to increase its value with each loop. To do this with index = 1, you get infinitive loop.

Plus you don't have to declare "i" outside of for statement

Nice job cassyjack, you doing well. Nice to see some mebers which do not only ask for code but actualy do their workload

Thanks for the info. Im taking Java II as a prereq and It's killing me. I had Java I about 4 years ago. I really appreciate your help and the explanation.

i dont know,....

commented: So do not post to old thread if you have no clue! -3
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.