定义结构体
c/c++中是这样的:
struct Node{
int a;
char c;
char c[8];
};
Node A;
Node B[10];
java中是这样:
class Node{
public int a;
public String s;
//加上一推set(),get(),Node(),Node(int a,String s);
}
Node A=new Node();
Node[] B=new Node[10];
注意这里若要使用这个B[0],需要再次new一次:
比如:
直接B[0].Node(1,"ss");是错误的。
必须这样:
Node[] B=new Node[10];
for(int i=0;i<10;i++){
Node[i]=new Node();
}
B[0].Node(1,"ss");
两种排序方法
一是在结构体内部重载比较函数,再调用Collections.sort或Arrays.sort进行排序。
调用方式为Arrays.sort(结构体数组名);
class Node implements Comparable<Node>{
public int a,b;
//下面为重载比较函数
public int compareTo(Node A){
if(this.a-A.a!=0){
return this.a-A.a;
}
else {
return this.b-A.b;
}
}
}
二是自定义比较类mycmp,通过xxx.sort(结构体数组名,起始,结束,new mycmp());进行排序
class Node{
int a,b;
}
class mycmp implements Comparator<Node>
{
public int compare(Node A, Node B)
{
if(A.a-B.a!=0)
return A.a-B.a;
else return A.b-B.b;
}
}