小编div*_*ero的帖子

Java Arrays.sort()方法需要一维数组,但我也可以传递一个二维数组,那为什么我不能做int [] a = b(其中b是一个二维数组)?

我很难理解我可以将2D数组传递给java的Arrays.sort()方法的概念.(我已经看过java文档,排序方法只能采用1D数组)
但是当我尝试下面的代码时得到错误

int[][] b={{1,2},{2,3}};
int[] a=b;
Run Code Online (Sandbox Code Playgroud)

但以下代码工作正常

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },{ 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
    return Integer.compare(o2[1], o1[1]);
}
});
Run Code Online (Sandbox Code Playgroud)

java arrays sorting

5
推荐指数
2
解决办法
780
查看次数

scanf之间的区别("%79 [^ \n]",行); vs scanf("%79 [^ \n] \n",行)vs scanf("%79 [^ \n] s",行)

我正在用c编写一个程序来读取文件的内容.代码如下:

   #include<stdio.h>
   void main()
   {
   char line[90];
   while(scanf("%79[^\n]\n",line)==1) 
   printf("%s",line);
   }
Run Code Online (Sandbox Code Playgroud)

上面的代码读取文件内容并将其显示在屏幕上.

while(scanf("%79[^\n]",line)==1) and while(scanf("%79[^\n]s",line)==1) or while(scanf("%79[^\n]s\n",line)==1) 
Run Code Online (Sandbox Code Playgroud)

不起作用.(它们只显示第一行)

谁能解释一下?

c scanf

3
推荐指数
1
解决办法
1349
查看次数

类型不兼容错误。发现需要 java.lang.Object :Item

我正在编写通用代码,如下所示:

class Link<Item>
   {
      public Item dData;
      public Link next;
      public Link(Item d)
       {
         dData=d;
       }
     public void displayLink()
       {
         System.out.println(dData);
       }
    }
   ............
   Link delete=first;
   1>Item temp=delete.dData;// This code gives the error(mentioned at the title)
   2>Item temp=(Item)delete.dData; //This code works fine (With a warning no error)
Run Code Online (Sandbox Code Playgroud)

为什么第 1 行给出错误 { 类型不兼容错误。发现 java.lang.Object required :Item },当下面的代码工作得绝对正常时?

  public class Stack<Item>
   {
     private Node first = null;
     private class Node
      {
        Item item;
        Node next;
      }
     public boolean isEmpty()
      {      return first …
Run Code Online (Sandbox Code Playgroud)

java generics

0
推荐指数
1
解决办法
4594
查看次数

Type参数的约束(类型设置为IComparable但无法将Type作为参数发送到采用IComparable的方法)

这是我的代码

public class UnOrderedMaxPQ<Key> where Key : IComparable
 {
    private Key[] array;
    private int N;
    public UnOrderedMaxPQ(int size)
      {
        array = new Key[size];
       }
         .......
     public Key DelMax()
      {  
InsertionSort.Sort(array);//Error cannot convert from 'Key[]' to IComparable[]' 

   return array[--N];
   }
  }
Run Code Online (Sandbox Code Playgroud)

这是插入排序代码

 public class InsertionSort
 {
     public static void Sort(IComparable[] array)
    {
        for (int i = 0; i < array.Length-1; i++)
        {
            for (int j = i + 1; j <=0;j--)
            {
                if (Less(array[j], array[i]))
                {
                    Exchange(array, j, i);
                }
                else …
Run Code Online (Sandbox Code Playgroud)

c# generics constraints

0
推荐指数
1
解决办法
208
查看次数

标签 统计

generics ×2

java ×2

arrays ×1

c ×1

c# ×1

constraints ×1

scanf ×1

sorting ×1