0
import java.util.ArrayList;

public class Paaohjelma {
    public static int pienin(int[] taulukko) {
        int temp, size;
        size = taulukko.length;

        for(int i = 0; i<size; i++ ){
            for(int j = i+1; j<size; j++){
                if(taulukko[i]>taulukko[j]){
                    temp = taulukko[i];
                    taulukko[i] = taulukko[j];
                    taulukko[j] = temp;
                }
            }
        }
        return taulukko[0];
    }
    
    public static int pienimmanIndeksi(int[] taulukko) {
        ArrayList<Integer> tauli = new ArrayList<>();
        
        for (int i : taulukko) {
            tauli.add(i);
        }
        
        return tauli.indexOf(Paaohjelma.pienin(taulukko));
    }
    
    public static int pienimmanIndeksiAlkaen(int[] taulukko, int aloitusIndeksi) {
        // this methods should get the index of smallest value starting from specified index
        int[] tempTauli = taulukko;
        tempTauli = new int[tempTauli.length - aloitusIndeksi];
        
        // this gets the right values to temporary array
        if (aloitusIndeksi > 0) {
            int index = 0;
            int indexTauli = 0;
            for(int value : taulukko) {
                if(index >= aloitusIndeksi) {
                    tempTauli[indexTauli] = taulukko[index];
                    indexTauli++;
                }
                index++;
            }
        }
        // values added are automatically sorted from smallest to largest?
        // this shouldn't be, array should be 5, 99, 3, 12 but is shown as 3, 5, 12, 99
        for(int inty : tempTauli) {
            System.out.println(inty);
        }
        
        // get the index of smallest value in array
        // index is 0 should be 2
        int index = Paaohjelma.pienimmanIndeksi(tempTauli);
        
        // return index of smallest value (add starting index to get the index of smallest value in the original array when starting from specified index)
        return index+aloitusIndeksi;
    }
    
    public static void main(String[] args) {
        // test code
        int[] taulukko = {3, 1, 5, 99, 3, 12};
        int minIndex = Paaohjelma.pienimmanIndeksi(taulukko);
        System.out.println("Pienin: " + Paaohjelma.pienin(taulukko));
        System.out.println("Pienimmän indeksi: " + minIndex);
        System.out.println(Paaohjelma.pienimmanIndeksiAlkaen(taulukko, 2));
    }

}

Hello! I'm doing some programming course work for school and have been stuck in this particular part for couple hours. So I decided it would be best for someone else to take a look and provide some light why my approach for this problem isn't working.

What should happen: class method PienimmanIndeksiAlkaen should return the index of smallest value in provided int array starting from specified index.

The main problem I have been having is that the array seems to be automatically sorting itself and I have no idea what is possible causing this. I have commented the relevant part of the code and would be more than happy if someone could explain why this is happening and what possible could be done to prevent this.

Murasaki
  • 105
  • 4
  • im confused, why do you do something like Paaohjelma.function when the functions are static – TruVortex_07 Mar 20 '21 at 18:43
  • @TruVortex_07 Test code comes straight from the course material, I have no idea. – Murasaki Mar 20 '21 at 18:46
  • The `pienin()` method sorts the array that you pass as a parameter. `pienimmanIndeksi()` calls the `pienin()` method with the array that you pass in, so after `Paaohjelma.pienimmanIndeksi(taulukko);` the `taulukko` array is sorted. – Thomas Kläger Mar 20 '21 at 18:50
  • @Thomas Kläger My blindness continues, thanks! – Murasaki Mar 20 '21 at 18:54

1 Answers1

2

The reason your array is sorted is when you call

    System.out.println("Pienin: " + Paaohjelma.pienin(taulukko));

you sort the array.

When you pass the array into this function, you aren't actually passing the value of the array, but the pointer to the array - the address of the array in memory. This is the difference between passing parameters by value or by reference.

enter image description here

How do you know if the value is passed by value or by reference? As a rule of thumb:

primitive values - i.e. int, double, etc. will be passed by value - their value will be copied and passed to the function.

Any other type, namely arrays and classes, will be passed by reference - the address of the value in memory will be passed to the function, thus any change to the value inside the function will affect it when the function ends too.

Read more here

krezno
  • 392
  • 1
  • 12
  • For objects, the references are _always_ passed [_by value_](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Nowhere Man Mar 20 '21 at 22:47
  • @AlexRudenko Yes, the array variable stores the pointer to the array, and so when we pass the array we actually pass the address by value. I think that at this level it's better to obfuscate the details a bit in order to understand the basic difference in behavior between primitives and objects The link i provided describes what you mentioned in detail. – krezno Mar 21 '21 at 09:52