如何在方法中对数组进行排序

时间:2014-05-26 19:19:51

标签: java arrays sorting methods

大家好!

我的程序编译成功完成......

问题在于方法total4 ...因为我在运行程序时在eclipse中进行编码,所以在名为total4的方法中出现了与“IF”一致的问题!

这是我的代码:

import java.util.Scanner;
public class Askisi1 {

    public static void main(String[] args) {
        Scanner ch=new Scanner(System.in);
        Scanner in = new Scanner(System.in);
        String[] onomata= new String[6];
        int[] pontoi1=new int[10];
        int[] pontoi2=new int[10];
        int[][] pontoi=new int [6][10];
        double[] taxinomisi=new double[6];

        int i,k;


            for (i=0; i<onomata.length; i++)
            {
                System.out.println("Parakalw doste to onoma tou"+" " +(i+1)+"ou"+" "+"paixti!");
                onomata[i] =  ch.nextLine();

            }

            for (i=0; i<=5; i++)
            {
                for (k=0; k<=9; k++)
                {
                    System.out.println("Parakalw doste tous pontous tou"+" " +(i+1)+"ou"+" "+"paixti ston"+" " +(k+1)+"o"+" "+"agwna!!");
                    pontoi[i][k]=in.nextInt();
                }
            }

            pontoi1=total1(pontoi);
            pontoi2=total2(pontoi);
            taxinomisi=total3(pontoi1);

            for (i=0; i<=5; i++)
            {
                System.out.println("Oi pontoi tou"+" "+(i+1)+"ou paixti einai"+" "+pontoi1[i]);
            }
            System.out.println(" ");
            for (i=0; i<=9; i++)
            {
                System.out.println("Oi pontoi tis omadas ston"+" "+(i+1)+"o agwna einai:"+" "+pontoi2[i]);
            }
            System.out.println(" ");

            for (i=0; i<=5; i++)
            {
                System.out.print(" "+total4(taxinomisi,onomata)+" ");
                System.out.println(onomata[i]);
            }


    }

        public static int[] total1 (int[][] array) {


                    int total=0;
                    int z=0; int j=0;
                    int[] a=new int[6];
                    for (z=0; z<=5; z++)
                    {
                        for (j=0; j<=9; j++)
                        {
                            total=total+array[z][j];
                        }
                        a[z]=total;
                        total=0;
                    }

                    return a;
                }

        public static int[] total2 (int[][] array) {
                    int total2 = 0; 
                    int z=0; int j=0; 
                    int[] b=new int[10];

                    do{

                    for (z=0; z<=5; z++)
                    {
                        total2=total2+array[z][j];

                    }
                    b[j]=total2;
                    total2=0;
                    j++;
                    }
                    while (j<=9);

                    return b;
                    }

        public static double[] total3 (int[] array){
            double mesos_oros=0.0;
            int z=0;
            double[] a=new double[6];
            for (z=0; z<=5; z++)
            {
                mesos_oros=array[z]/10;
                a[z]=mesos_oros;
                mesos_oros=0.0;
            }
            return a;

        }

        public static double[] total4 (double[] array,String[] array2){
            String y=" ";
            double x=0.0;
            double max=array[0];
            for (int i=0; i<=5; i++)
            {
                if (array[i+1]>max)
                {
                    array[i]=x;
                    array[i+1]=array[i];
                    array[i+1]=x;
                    array2[i]=y;
                    array2[i+1]=array2[i];
                    array2[i+1]=y;

                }
            }

            return array;
        }

}   

我只是想对数组进行排序,但我看不出任何错误......

提前致谢!!

1 个答案:

答案 0 :(得分:1)

问题在于if (array[i + 1] > max),更具体地说是array[i+1]

你在这一行打电话给total4

System.out.print(" " + total4(taxinomisi, onomata) + " ");

所以传入的arraytaxinomisi

您在此设置taxinomisitaxinomisi = total3(pontoi1);

total3中你返回:

double[] a = new double[6];
// stuff
return a;

所以你将taxinomisi(一个长度为6的数组)传递给total4。现在让我们看一下total4中的循环:

for (int i = 0; i <= 5; i++) {
    if (array[i + 1] > max) 

我们在这里可以看到,当i为5时,array[i+1]将在数组中查找元素6。但是,Java中的数组是从零开始的:它们从0开始。所以你实际上是在寻找数组中的第7个元素(如果我们从1开始)。但是,该数组只有6个元素,导致ArrayIndexOutOfBounds异常。

实际上,错误消息告诉您:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at stuff.Stuff.total4(Stuff.java:106)
    at stuff.Stuff.main(Stuff.java:45)

Java Result: 1