proste zadanie z tablica

0

polecenie:
method that returns the position of the highest value in the array, or -1 if the array is empty.If the highest value occurs more than once, return the position of its first occurrence in the array.

[1,2,3,0,-1] should return 2.
[1,2,2,1] should return 1.
[] should return -1.

        int tmp = 0;
        int tmpMax = 0;
        int howManyTimesAppeared = 0;
        int maxTimesAppeared = 0;
        int firstAppearance = 0;
        int currentAppearance = 0;
        try {
            if (array.length == 0) {
                return -1;
            } else {
                for (int i = 0; i < array.length; i++) {
                    if(array[i]>tmp){
                        tmp=array[i];
                        howManyTimesAppeared++;
                        currentAppearance=i;
                        if(howManyTimesAppeared>maxTimesAppeared){
                            maxTimesAppeared=howManyTimesAppeared;
                            firstAppearance=currentAppearance;
                        }
                    }
                }
            }
        } catch (UnsupportedOperationException uo) {
            System.err.println("Unsupported Exception found");
        }
        return firstAppearance;
    }

mam cos takiego i czegos mi brak :P

0

Skoro jest proste to po co nam zawracasz głowę?

0

Wystarczą Ci 2 zmienne:
highest - dla największej dotychczasowej wartości, oraz index - dla indeksu tej wartości.

Jeżeli w for będziesz miał:

if (array[i] > highest) {
	highest = array[i];
	index = i;
}

To masz pewność że index będzie indeksem pierwszego pojawienia się największej liczby, bo jeśli drugi raz pojawi się liczba o tej samej wartości co highest wtedy array[i] == highest a to nie spełni warunku w if.

Ten try-catch również wydaję mi się zbędny.

0

tak działa dzięki, a teraz do drugiego zadania podpowiedz potrzeba :D
polecenie:
method that returns the starting position of the last occurence of the subArray within the array, or -1 if the subArray cannot be found.
przykłady wynikow:
[4,3,3,7,8] and [3,7] should return 2.
[1,3,5] and [1] should return 0.
[7,8,9] and [8,9,10] should return -1.
[0,0,3,7,0,3,7,0] and [3,7] should return 5.

public static void main(String[] args) {
        int[] array = new int[]{7,8,9};
        int[] subArray = new int[]{8,9,10};
        System.out.println(findArray(array,subArray));
    }

    public static int findArray(int[] array, int[] subArray) {
        int firstOccurance= 0;
        try {
            if (checkingIfSubArrayFullyListedInArray(array, subArray)) return -1;


        } catch (UnsupportedOperationException uoe) {
            System.err.println("UnsupportedOperationException found");
        }
        return 0;
    }


    private static boolean checkingIfSubArrayFullyListedInArray(int[] array, int[] subArray) {
        int counter = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < subArray.length; j++) {
                if(array[i]==subArray[j]){
                    counter++;
                }
            }
        }
        if(counter!=subArray.length){
            return true;
        }
        return false;
    }

zrobilem tylko 1/4podpunkty :f pomysly jak sie zajac reszta?

1 użytkowników online, w tym zalogowanych: 0, gości: 1