Home > Back-end >  I need to print the largest element of an Array, but showing its index number
I need to print the largest element of an Array, but showing its index number

Time:01-26

I have a fixed variable of 50.I have two different Arrays (with fixed numbers).I need to sum the first variable (50) to the elements of the first Array and divide the result by each element of the other Array. Then I need to print the largest number of that Array, but showing the index number (which should be 2). I can't figure out how to pull the index by refering to the largest number.

My code shows the index by using hardcode, I need to fix that, can you help me?

public static void main(String[] args) {

        int h = 50;
        int d [] = { 10, 25, 5 };
        int z [] = { 2, 3, 1 };

        String result = "";

        for (int i = 0; i < d.length; i  ) {
            result  = ((h   d[i]) / z[i])   ",";
        }

        String str = result;

        String[] string = str.replaceAll("\\[", "").replaceAll("]", "").split(",");

        int[] arr = new int[string.length];

        for (int i = 0; i < string.length; i  ) {
            arr[i] = Integer.valueOf(string[i]);
        }

// CAN'T USE HARD CODE HERE, PLEASE HELP
        Arrays.stream(arr).max().getAsInt();
        System.out.println("The largest number index is: "   findIndex(arr, 55)); 

    }

    public static int findIndex(int arr[], int t) {

        ArrayList<Integer> list = new ArrayList<>();

        for (int i : arr)
            list.add(i);
        return list.indexOf(t);
    }
}

CodePudding user response:

I dont understand why are you storing result as string and then converting this string into array of int. You can directly save int array.

public static void main(String[] args) {

    int h = 50;
    int d [] = { 10, 25, 5 };
    int z [] = { 2, 3, 1 };
    int result [] = new int[d.length];
    int max = -1;
    int indexMax = -1;


    for (int i = 0; i < d.length; i  ) {
        result[i] = ((h   d[i]) / z[i]);
        if(result[i]>max){
            max = result[i];
            indexMax = i;
        }
    }
    
    System.out.println(max);
    System.out.println(indexMax);
    

}

CodePudding user response:

Here is my solution to your problem

this answer is generic (you can change the values of arrays, but it must be equals in index)

import java.util.Arrays;
import java.util.Collections;

public class Main{
    public static void main(String[] args) { 

        int h = 50;
        int d [] = { 10, 25, 5 };
        int z [] = { 2, 3, 1 };
        
        
        // here we sum every element of the first array to variable h (that equals 50)
        
        for (int i = 0 ; i < d.length ; i  ){
            d [i] = d[i]   h ;
        }
        
        // here we divide every element of the first array to other array .

        for (int i = 0 ; i < d.length ; i  ){
            d [i] = d[i] / z[i] ;
        }
        
        // here we want to find the max 
            int max = Arrays.stream(d).max().getAsInt();
            
        // here we get the index of the max value
         int index = 0;
         
         for (int i = 0 ; i < d.length ; i  ){
            if (d[i] == max){
                index = i;
                break;
            } 
        }
        
        System.out.println("The largest number index is: "   index); 
        
}}
  •  Tags:  
  • Related