我很难理解我可以将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) 我正在用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)
不起作用.(它们只显示第一行)
谁能解释一下?
我正在编写通用代码,如下所示:
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) 这是我的代码
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)