Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By aconti

Thread: how to read array index

  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default how to read array index

    Hello,

    I need to read an array index but I am getting the value within the array index:

    Java Code:
    	int diff = 0;
    		int lowest = 0;
    		int ans =200;
    		int position = 0;
    
    		int array1[] = {1,200,8,78,99};
    		
    		for(int i = 0; i < 4; i++){
    		 
    			diff = Math.abs(array1[i] - array1[i + 1]); 
    			
    		    if(ans > diff){
    		    	ans = diff;
    		    	position = array1[i];
    		    	                  
    				}
    I need to store the array index into the variable position

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What is your goal exactly? In the array
    Java Code:
    int array1[] = {1,200,8,78,99};
    The indices go 0,1,2,3,4

  3. #3
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    Yes I want to read the index and not the value within the index

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    i is the index in your for loop.

  5. #5
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    but when I code:

    position = array1[i];

    the value within the index is stored in the variable position and not the index itself

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

Similar Threads

  1. Array index out of range: 1 HELP!
    By TheBreadCat in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 08:50 PM
  2. Using variable as array index
    By SalParadise in forum New To Java
    Replies: 9
    Last Post: 11-16-2010, 09:56 PM
  3. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  4. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 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
  •