The method sort(Comparable[]) in the type Shell is not applicable for the arguments (int[])

看算法第四版的希尔排序时,打算把例中的String数组换成int数组,却出现了上面的问题。最后在StackOverflow找到答案
原代码:

package sort;

import edu.princeton.cs.algs4.In;

public class Shell{
    public static void sort(Comparable[] a)
    {    //将a[]升序排列
        int N = a.length;
        int h = 1;
        while (h<N/3) h = 3*h + 1; // 1,4,13...
        while (h>=1)
        {    // 数组变为h有序
            for (int i=h;i<N;i++)
            {    // 将a[i]插入到a[i-h],a[i-2h]..中
                for (int j=i;j>=h&&less(a[j],a[j-h]);j -= h)
                {
                    System.out.println(a[j]+" "+a[j-h]+less(a[j],a[j-h]));exch(a,j,j-h);
                    for (int k=0;k<N;k++)
                        System.out.print(a[k]+" ");
                    System.out.println();
                }
            }
            h = h/3;
        }
    }
    private static boolean less(Comparable v,Comparable w)
    // 比较元素
    {      return v.compareTo(w)<0;    }
    private static void exch(Comparable[] a,int i,int j)
    // 将元素交换位置
    {    Comparable t = a[i]; a[i] = a[j]; a[j] = t;    }
    private static void show(Comparable[] a)
    {    //  单行打印数组,只有本类的静态方法可调用
        for (int i=0;i<a.length;i++)
        System.out.println();
    }
    public static boolean isSorted(Comparable[] a)
    {  // 判断数组元素是否有序
        for (int i=1;i<a.length;i++)
            if (less(a[i],a[i-1]))  return false;
        return true;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 从标准输入读取字符串,将它们排序并输出
        String[] a = In.readStrings();
        sort(a);
        assert isSorted(a); // 确认有序
        show(a);

    }
}

String[] a = In.readStrings()不能改成int[] a = In.readInts(),因为int是原始数据类型,无法实现接口。应该改成Integer[],然而In类没有对应的方法,所以我改成了

        int[] in = In.readInts();
        Integer[] a = new Integer[in.length];
        for (int i=0;i<a.length;i++)
            a[i]=in[i];
        sort(a);
        assert isSorted(a); // 确认有序
        show(a);

测试有效

Stackoverflow地址

原文地址:https://www.cnblogs.com/hesfail/p/12357457.html

时间: 2024-10-08 17:47:01

The method sort(Comparable[]) in the type Shell is not applicable for the arguments (int[])的相关文章

The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

写了一段测试代码运行时一直报错:The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String) 测试代码: driver.findElement(By.name("wd")).sendKeys("selnium"); 原因:旧版本的Java不理解非随机变量参数 解决方法:在工程上点击右键选择Build Path -> 

错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

Fragment newfragment =new MyFragment();fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit(); 提示错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment) 妈蛋,找了好久!一直以

The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)

1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: 444 in the jsp file: /WEB-INF/pages/countOrder/viewCountOrderDetails.jsp The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object) 441: </t

The method load(Class, Serializable) in the type Session is not applicable for the arguments (Class&lt;

Transaction transaction = session.beginTransaction(); //load是通过主键属性,获取对象的实例 Employee employee  =(Employee) session.load(Employee.class, 1); employee.setName("demo"); transaction.commit(); session.close(); 报错The method load(Class, Serializable) i

The method load(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments (Class, int)

引入别人的项目发现利用HibernateTemplate的load的方法报错了.错误提示为: 意思为load方法的第二个参数是实现Serializable接口的对象,int类型不符合.但jdk自动装箱,int会自转换为Integer,而Integer是实现了Serializable的,所以应该是可以的.但myeclipse偏偏报错了,原因是我的myeclipse中window->preferences->java-compiler中的Compiler compliance level设置为1.

The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the

 The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String) 今天遇到这样一个很奇葩的错误信息,后来查到我导入的包有问题  import android.app.Fragment; import android.app.FragmentManager;   其实我应该使用的

Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not access

准备使用Java进行图片压缩的时候,使用 import com.sun.image.codec.jpeg.*; 结果出现错误: Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library 上网查了一下,发现是IDE的设置问题,它默认把这些受访问限制的API设成了ERROR

The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments

The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (String, new   View.OnClickListener(){}) .setNegativeButton("Don't Remind", new OnClickListener() .setNegative

Mapper method &#39;com.autoyol.mapper.trans.AccountLogMapper.getTotalIncomByMemNoLastest attempted to return null from a method with a primitive return type (int).解决方法

1.打开日志输出,降低日志级别. <AppenderRef ref="console" level="trace"></AppenderRef> 否则看不到报错.. 2.调整mysql语句,不能使用order by limit之类的 <!-- SELECT IFNULL(amt,1) FROM account_log WHERE mem_no=#{value} AND income_type != 4 ORDER BY id DESC