Array trouble


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Array trouble

  1. #1
    Join Date
    Nov 2003
    Posts
    3

    Angry Array trouble

    Hey,
    can someone help me with a problem im having

    i have an array with 5 int. i need to be able to find the
    maximum value thats in the 5 int. and also that values index

    thanks in advance

  2. #2
    Join Date
    Nov 2003
    Posts
    29
    the binarysearch() is agood one

    int indexPos = Arrays.binarySearch(yorArray, intToFind);

    requires arguments of yor array, and what u want to find and will return elemnt position if found.

    You need to sort yor array first

    Arrays.sort(yorarray);

  3. #3
    Join Date
    Nov 2003
    Posts
    3
    ok im new to java,
    how exactly do i do the sort?

  4. #4
    Join Date
    Nov 2003
    Posts
    29
    i think i understand what u want to do now - the binary search is porbly no good for you. This can solve your problem but there are much more economical ways to do so . .

    Code:
    import java.util.*;
    class find
    {
    public static void main(String args[]) {
    
    int place5;
    int rNumber;
    
    int[]myArray = new int[5];
    	for (int i=0;i<5;i++)
    	{
    	rNumber = (int)(Math.random()*10+1);
    	myArray[i]=rNumber;                // populate yor array with 5 random int (1-10)
    	System.out.print(myArray[i]+" ");  //print out array
    	}
    	
    Arrays.sort(myArray);   //sort array in ascending numerical order
    System.out.println();   
    
    for (int i=0;i<5;i++)
    	{
    int y = (i);
    System.out.print(y +": " + myArray[i] + " ");  //print out sorted array
    if (y==(myArray.length -1))
    		{
    	System.out.println("\nHighest number in array is " + myArray[4] + " at index " + y);	
    		}
    
    	}
    }
    }
    I was looking myself for the syntax to return an elements index value, binarySearch can do this, but in this case you are not actually searching. Anyone know the syntax to return an elements index value?

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    It depends exactly on what your specifications are. I think sorting the array is a bit extreme without know the requirements and consequences of that action.

    Below you will find code to find the value of the index of the maximum value in the array. I've included comments so it should be reasonbly easy to understand, but if you've got any problems or this isn't what you want, post again.
    Code:
    public class Test {
            public static void main(String[] args) {
                    int[] myArray = new int[] {4, 7, 2, 8, 1};
                    int maxValueIndex = findMaxIndex(myArray);
    
                    System.out.println("Max Value: " + myArray[maxValueIndex]);
                    System.out.println("Index:     " + maxValueIndex);
    
            }
    
            public static int findMaxIndex(int[] array) {
                    int indexOfMax = 0;
    
                    if (array.length == 0) {
                            // Decide what you're going to do if you're passed an empty array.
                            // Personally, I'd throw an exception or return -1.
                    }
    
    
                    // Loop through each array element.
                    for (int index = 0; index < array.length; index++) {
    
                            // If the contents of the current element is greater than the contents of our current max.
                            if (array[index] > array[indexOfMax]) {
    
                                    // Store the current index as our maximum.
                                    indexOfMax = index;
    
                            }
                    }
    
                    return indexOfMax;
            }
    }
    ArchAngel.
    O:-)

  6. #6
    Join Date
    Nov 2003
    Posts
    29
    hmm, maybe ive been missing something. Was the findMaxIndex() a method of your own creation, becos i found these classes:
    http://www.dickinson.edu/~braught/co...DieTotals.html

    and when i try to use findMaxIndex() without code iinside it dosn't work. im presuming its not an 'inbuilt' java method (what are they called?) which is why you had to code the method. .

    please tell me thats right

  7. #7
    Join Date
    Mar 2003
    Posts
    834
    > hmm, maybe ive been missing something. Was the findMaxIndex() a method of your own creation,

    err...yeah...you can see the definition:
    Code:
             System.out.println("Index:     " + maxValueIndex);
    
            }
    
            public static int findMaxIndex(int[] array) {
                    int indexOfMax = 0;
    
                    if (array.length == 0) {
                            // Decide what you're going
    > becos i found these classes: http://www.dickinson.edu/~braught/c...tDieTotals.html

    That's fine if (1) you have access to the classes (2) you're not having to write it yourself for work, coursework or for a programming exercise.

    > and when i try to use findMaxIndex() without code iinside it dosn't work.

    I don't quite understand what you mean. Do you mean "It won't work unless I have defined a method called findMaxIndex()". If that's the case then of course it's not going to work! It's like typing:
    Code:
      String str = getMeaningOfLife();
    If you don't have the method 'getMeaningOfLife()' defined, you won't be able to execute it.

    > im presuming its not an 'inbuilt' java method (what are they called?) which is why you had to code the method.

    They're called JFC (Java Foundation Classes), but a lot of people jast call them "Java Libraries" or the "Java API".

    Yeah, there isn't anything that exactly does this in the JFC (to my knowledge - there are quite a lot of classes), so that's why I wrote it.
    ArchAngel.
    O:-)

  8. #8
    Join Date
    Nov 2003
    Posts
    29
    ha ha if only that worked. well i was just checking and i only just realised you can make documentation of your class that look like they are in the java libaries. I thought if that wasn't a custom method then i had really misunderstood

  9. #9
    Join Date
    Mar 2003
    Posts
    834
    yup - s'called JavaDoc.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center