• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding out the max number in Array  RSS feed

 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've been working with arrays a bit, and came up with a bit of obstacle. I need to find out the largest number in an Array.


The code checks one number of Array, then compares it to previous one. Which number is bigger, gets assigned to variable max. The problem is that Array's index start at 0, so at the start I get: java.lang.ArrayIndexOutOfBoundsException: -1

I understand that the code tries to check Second[-1], which doesn't exist, then show should I go about fixing this ?

Thanks.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited this because I was partially wrong at the beginning and I was seeing that nobody quoted me or posted after me

In order to avoid this negative index problem you could start your loop beggining with i=1. But this would not work as expected!
Suppose your array is 7 3 4 5 3 4.
The max value is obviously 7.
What do you do in the last step (i = 5)?
You compare 4 against 3 and set max to bigger of those. That would be 4.
But the max value is 7!

Do not compare Second[i] against Second[i-1]. Compare it against max. At start max should be Integer.MIN_VALUE (or Second[0], that would work, too).


A tip. By convention, variable names in Java should start by lower case letter.

Why are you modyfying your array while trying to find max value (line 3)?
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:One option: Set max to Second[0] and start your loop from index 1.
An alternative: Do not compare Second[i] against Second[i-1]. Compare it against max. At start max should be Integer.MIN_VALUE.

A tip. By convention, variable names in Java should start by lower case letter.

Why are you modyfying your array while trying to find max value (line 3)?

I'm creating that Array in for loop. I don't need any specific value on my Array, so I just add 1++ to the next index. I'll try something, thanks.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:I'm creating that Array in for loop. I don't need any specific value on my Array, so I just add 1++ to the next index. I'll try something, thanks.

Sorry, I don't get it. Are you finding max value of an array or are you initializing it? What are you trying to achieve?
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Akimbas Akimbasas wrote:I'm creating that Array in for loop. I don't need any specific value on my Array, so I just add 1++ to the next index. I'll try something, thanks.

Sorry, I don't get it. Are you finding max value of an array or are you initializing it? What are you trying to achieve?

My goal is to find the max number of all numbers in an Array, but I don't have specific list of numbers. I didn't want to write Second[0] = number, Second[1] = another number etc., so I used for loop for that. My initial value of k_2 = 7, it gets bigger by 1++ after the loop. So my Second[i] = 7 Second[i+1] = 8 and so on. Am I creating this the hard way ? I am doing this for purpose of understanding how to do things with arrays, that's why the code may look a bit.. well.. silly.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I suggest you fill the array using one method and find the max value using other method. Do not mix those.
I recommend that you test your max method against an array filled with random data, not an incrementing sequence. You can fill an array using random data using a loop .
Your algorithm was wrong (I fell for that trap ) but you wouldn't spot the mistake using an array where max value was on the last position.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:The code checks one number of Array, then compares it to previous one. Which number is bigger, gets assigned to variable max. The problem is that Array's index start at 0, so at the start I get: java.lang.ArrayIndexOutOfBoundsException: -1
I understand that the code tries to check Second[-1], which doesn't exist, then show should I go about fixing this ?

Well, one way would be to keep two indexes simultaneously, viz:
for (int i = 0, j = 1; j < array.length; i++, j++) { ...

However, as Pawel says, the way you're going about this is wrong. The fact is that you don't need to compare consecutive elements (which appears to be what you're doing) in order to find the largest one.

My suggestion: StopCoding (←click), and write out, with a paper and pencil, what will happen with your current logic. Then see if you can work out (also on paper) what you should be doing.

Winston
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:My suggestion: StopCoding (←click), and write out, with a paper and pencil, what will happen with your current logic. Then see if you can work out (also on paper) what you should be doing.

And use some random data like 3 7 1 3 4 1 to do that. Don't use incrementing sequence like 1 2 3 4 5 6...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!