Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 排序和显示对象数组_Arrays_Sorting_Object - Fatal编程技术网

Arrays 排序和显示对象数组

Arrays 排序和显示对象数组,arrays,sorting,object,Arrays,Sorting,Object,我对编程有点陌生,我正在尝试显示打印的对象数组。我一直遇到这个编译错误: SchoolTextBookSort.java:90:错误:找不到符号 System.out.println(教科书[x].bubbleSortAuthor()+“”); ^ 符号:方法bubbleSortAuthor() 地点:班级课本 1错误 下面是出错的代码。有人能帮我吗 if (response.equals("AUTHOR")) { for (int x = 0; x < t

我对编程有点陌生,我正在尝试显示打印的对象数组。我一直遇到这个编译错误:

SchoolTextBookSort.java:90:错误:找不到符号 System.out.println(教科书[x].bubbleSortAuthor()+“”); ^ 符号:方法bubbleSortAuthor() 地点:班级课本 1错误

下面是出错的代码。有人能帮我吗

    if (response.equals("AUTHOR"))
    {
        for (int x = 0; x < textBooks.length; ++x)
        {
            System.out.println(textBooks[x].bubbleSortAuthor() + " ");
        }
        System.out.println();
    }
    else
        System.out.println("Invalid Response.");
}

public static void bubbleSortAuthor(SchoolTextBook[] array)
{
    int a, b;
    SchoolTextBook temp;
    int highSubscript = array.length - 1;

    for (a = 0; a < highSubscript; ++a)
        for(b = 0; b < highSubscript; ++b)
            if (array[b].getAuthorName().compareTo(array[b + 1].getAuthorName()) < 0)
            {
                temp = array[b];
                array[b] = array[b + 1];
                array[b + 1] = temp;
            }               
}
if(response.equals(“AUTHOR”))
{
用于(int x=0;x<0.length;++x)
{
System.out.println(教科书[x].bubbleSortAuthor()+“”);
}
System.out.println();
}
其他的
System.out.println(“无效响应”);
}
公共静态void bubbleSortAuthor(教科书[]数组)
{
INTA,b;
学校课本;
int highSubscript=array.length-1;
对于(a=0;a
您需要调用bubbleSortAuthor,方法是将数组传递给它,而不是在数组中调用它

像这样:

bubbleSortAuthor(textBooks);
它是静态的,但以数组作为参数,并且是属于您所在类的方法,因此您不需要

YourClassName.bubbleSortAuthor(textBooks);
同班同学


此外,它返回void,您不能打印void。您应该在数组排序后再次循环打印它。

您有一些小错误,如下所述

  • 您没有将
    教科书
    数组传递给方法
    bubbleSortAuthor(教科书[])
    。似乎您正试图对数组中的SchoolBook对象调用该方法
  • 不能从类对象调用静态方法。你要么像这样称呼它

    bubbleSortAuthor(array);                       // if calling it from inside the class 
    
    或者使用类名

    SchoolTextBook.bubbleSortAuthor(array);       // from anywhere - if declared public
    
  • 您的冒泡排序算法不正确。内部for循环应写为:

    for(b = 0; b < highSubscript -a; ++b) {       // you were missing the -a
    

    变量<代码>教科书<代码> <代码>数组<代码>还是数组<代码>(2D数组)的<代码>数组?如果本页上的答案解决了您的问题,请考虑将其标记为接受。谢谢(接受也可获得积分)
    if (response.equals("AUTHOR")) {
        for (int x = 0; x < textBooks.length; ++x) {
            // if textBooks is 2D array of SchoolTextBook objects then call it like this
            bubbleSortAuthor(textBooks[x]);
        }
    
        // if textBooks is 1D array of SchoolTextBook objects then you don't need the
        // above for loop. You could just write: 
        // bubbleSortAuthor(textBooks);
    
        // print results
        for(SchoolTextBook textBook : textBooks) {
            System.out.println(textBook.getAuthorName());
        }
    } else {
        System.out.println("Invalid Response.");
    }
    
    public static void bubbleSortAuthor(SchoolTextBook[] array) {
        int a, b, arrayLength = array.length;
        SchoolTextBook temp;
    
        for (a = 0; a < arrayLength - 1; ++a) {
            for(b = 0; b < arrayLength - 1 - a; ++b) {
                if (array[b].getAuthorName().compareTo(array[b + 1].getAuthorName()) > 0) {
                    temp = array[b];
                    array[b] = array[b + 1];
                    array[b + 1] = temp;
                }
            }
        }
    }