Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. Number Array

So I have an array of values:
dht = new int[] { 41, 68, 77, 74, 74, 68, 88 };

And I want to return the index/of the position of the highest number. In this case 88.
I understand it is in the 6th position.

So,
```public static int hottestDay( int[] temp) {
int hottest = 88;
for(int i; i < dht.length ; i++){
if( dht[i] == hottest)
return i;
}```

I am more interested in how you solve, than the actual answer itself. If someone could get on AIM and walk me through this and other stuff, I would be so happy.

2. Re: Number Array

```        int[] test = {1, 2, 3, 4};
int highestVal = 0;
int position = -1;
for (int i = 0; i < test.length; i++) {
if (test[i] > highestVal) {
highestVal = test[i];
position = i; //Kinda tells position of highest val
}

}
if (highestVal == 0 || position == -1) {
System.out.println("Test Failed?");
} else {
System.out.println("Highest Array Element is " + highestVal);
}```

That might be what you're kind of after? It stores highest value so far and its position.

3. Re: Number Array

Kinda of.
```int[] test = {1, 2, 3, 4};
int highestVal = 0;

int position = -1;
for (int i = 0; i < test.length; i++) {
if (test[i] > highestVal) {
highestVal = test[i];
position = i; //Kinda tells position of highest val
}

}
if (highestVal == 0 || position == -1) {
System.out.println("Test Failed?");
} else {
System.out.println("Highest Array Element is " + highestVal);
}

return position;
}```

That would work, but what if
`int[] test = {-5,-4,-3,-2,-1};`

It'll return position -1.
However, the highest value would be -1, which is at position 4.

4. Re: Number Array

Change newbie's code to set it to set highest value to the first index of the array. Then compare it like before. This way, it can handle negatives.

5. Re: Number Array

indices != values

newbie's code almost works. The only case it won't work is if the highest value happens to be 0. There's no need to store the highest value and the position. Furthermore, except for an array of size 0 there is always guaranteed to be at least one value which is the largest (or in this case, most positive). How you handle multiple highest values in the list is up to you, though usually you would just return the first one you find.

```public static int max(int[] numbers)
{
if(numbers.length == 0)
{
return -1; // no numbers to find the max of
}
int highestIndex = 0;
for(int i = 1; i < numbers.length; ++i)
{
if(numbers[i] > numbers[highestIndex)
{
highestIndex = i;
}
}
return highestIndex;
}```

Test cases:
```System.out.println(max(new int[]{1, 2, 3, 4})); // prints 3
System.out.println(max(new int[]{})); // prints -1
System.out.println(max(new int[]{-1, 0})); // prints 1
System.out.println(max(new int[]{-1, -3, -6 ,-1, 3, 100, 50})); // prints 5```