Results 1 to 3 of 3
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Can somoene explain this to a noob?

    Java Code:
        public static void main(String[] args)
        {
        	int[] myNum={476, 717, 33, 408, 9295};
        	int maxIndex = findMax(myNum);
        	System.out.println("Max is= " +myNum[maxIndex] +" " +maxIndex);
        }
        
        public static int findMax(int[]num)
        {
        	int maxNum=num[0];
        	int index=0;
        	
        	for(int i=0; i<num.length;i++)
        	{
    			if(num[i]>maxNum)
    			{
    				maxNum=num[i];
    				index=i;
    			}    		
        	}  return index;  
        }
        
    }
    Output:
    Max is= 9295 4

    This code compiles and checks for the largest number in the array, the output shown is the maxnumber returned and the index number. I want to know why does the index number return 4, instead of 5?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What is the first index number of any Java array?

    edit: hint: look at your for-loop. What indices does it use loop with?
    Last edited by Fubarable; 01-14-2009 at 06:37 AM.

  3. #3
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 08:38 AM
  2. Need Help Can anyone explain what this means
    By Clemenza1983 in forum New To Java
    Replies: 6
    Last Post: 02-16-2008, 03:13 AM
  3. Can anyone briefy explain what does that mean?
    By Clemenza1983 in forum New To Java
    Replies: 6
    Last Post: 01-29-2008, 07:05 AM
  4. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 11:35 AM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •