简体   繁体   English

Int 数组自行排序

[英]Int array sorting itself

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.应该发生什么:class 方法 PienimmanIndeksiAlkaen 应该从指定索引开始返回提供的 int 数组中最小值的索引。

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.我已经评论了代码的相关部分,如果有人能解释为什么会发生这种情况以及可以采取哪些措施来防止这种情况发生,我将非常高兴。

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 .当你把数组传给这个function时,你实际上传递的不是数组的值,而是指向数组的指针——memory中数组的地址。这就是按值传参和按引用传参的区别。

在此处输入图像描述

How do you know if the value is passed by value or by reference?您如何知道值是按值传递还是按引用传递? As a rule of thumb:根据经验:

primitive values - ie int, double, etc. will be passed by value - their value will be copied and passed to the function.原始值——即 int、double 等将按值传递——它们的值将被复制并传递给 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.任何其他类型,即 arrays 和类,将通过引用传递 - memory 中值的地址将传递给 function,因此当 function 结束时,对 function 中值的任何更改都会影响它。

Read more here 在这里阅读更多

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.应该发生什么: class 方法 PienimmanIndeksiAlkaen 应该从指定的索引开始返回提供的 int 数组中最小值的索引。

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.我已经评论了代码的相关部分,如果有人能解释为什么会发生这种情况以及可以采取哪些措施来防止这种情况发生,我会非常高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM