Array trouble

• 11-07-2003, 05:23 AM
bharrison4
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

• 11-07-2003, 08:23 AM
rix
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);
• 11-08-2003, 01:40 AM
bharrison4
ok im new to java,
how exactly do i do the sort?
• 11-08-2003, 06:54 AM
rix
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?
• 11-08-2003, 04:36 PM
ArchAngel
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;         } }```
• 11-10-2003, 10:50 AM
rix
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 :)
• 11-10-2003, 11:54 AM
ArchAngel
> 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.
• 11-10-2003, 12:26 PM
rix
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
• 11-10-2003, 04:29 PM
ArchAngel