内部类与ListNode的使用方法
内部类的使用方法
// An highlighted block
public class Main2 {
class ListNode{
int val;
ListNode next;
ListNode(int x){
val = x;
}
}
public static void main(String[] args) {
Main2 mm = new Main2();
Main2.ListNode node = mm.new ListNode(5);//需这样定义方可
System.out.println(node.val);
}
}
```javascript
// An highlighted block
var foo = 'bar';
对于常用的结构体比较方法:
// An highlighted block
package my.la;
import java.util.Arrays;
import java.util.Scanner;
class student implements Comparable<student>{
int no;
int score;
public student(int no, int score) {
this.no = no;
this.score = score;
}
public int compareTo(student a) {
if(a.score != this.score) return this.score - a.score;
else return this.no - this.no;
}
}
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
student[] st = new student[10];
for(int i = 0; i< n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
st[i] = new student(x, y);
}
Arrays.sort(st,0, n);
for(int i = 0; i< n; i++) {
System.out.println(st[i].no + " " + st[i].score);
}
}
}
可以选择容器,不用生成类数组:
package newOne;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
class student implements Comparable<student>{
int no;
int score;
public student(int no, int score) {
this.no = no;
this.score = score;
}
public int compareTo(student a) {
if(a.score != this.score) return this.score - a.score;
else return this.no - this.no;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<student> list = new ArrayList<>();
//student[] st = new student[10];
for(int i = 0; i< n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
//st[i] = new student(x, y);
student tmp = new student(x, y);
list.add(tmp);
}
//Arrays.sort(st,0, n);
Collections.sort(list);
/*for(int i = 0; i< n; i++) {
System.out.println(st[i].no + " " + st[i].score);
}*/
for(student stu: list){
System.out.println(stu.no + " " + stu.score);
}
}
}
同时还可以实现接口来解决这个问题:(可参考另一个,pat题解)
package newOne;
import java.util.*;
class student {
int no;
int score;
public student(int no, int score) {
this.no = no;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<student> list = new ArrayList<>();
//student[] st = new student[10];
for(int i = 0; i< n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
//st[i] = new student(x, y);
student tmp = new student(x, y);
list.add(tmp);
}
//Arrays.sort(st,0, n);
Collections.sort(list, new Comparator<student>() {
@Override
public int compare(student a, student b) {
if(a.score != b.score) return a.score - b.score;
else return a.no - b.no;
}
});
for(student stu: list){
System.out.println(stu.no + " " + stu.score);
}
}
}
对二维数组进行排序
public int[][] merge(int[][] inter) {
List<int[]> res = new ArrayList<>();
if(inter == null || inter.length == 0)
return res.toArray(new int [0][0]);
Arrays.sort(inter, new Comparator<int[]>() {
@Override
public int compare(int[] a, int [] b) {
return a[0] - b[0];
//对每一个开头进行排序
}
});
}
对于使用ArrayList的二维数组排序
Collections.sort(res, new Comparator<ArrayList<Integer>>() {
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
if (o1.size()<o2.size()){
return 1;
}else return -1;
}
});//按照长度递减
4.对于数组,自定义规则排序
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
import java.util.*;
public class Solution {
public String PrintMinNumber(int [] num) {
int len = num.length;
if(len == 0) return "";
String[] st = new String[len];
/*for(int i = 0; i< len; i++){
st[i] = String.valueOf(num[i]);
}
Arrays.sort(st, new Comparator<String> (){
@Override
public int compare(String a, String b){
String r1 = a + b;
String r2 = b + a;
return r1.compareTo(r2);
}
});*/
Integer[] inter = new Integer[len];
for(int i = 0; i< len; i++){
inter[i] = num[i];//必须转换,不然会报错,数组无法实现自动装箱的功能
}
Arrays.sort(inter, new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b){
String s1 = a + "" +b;
String s2 = b + "" + a;
return s1.compareTo(s2);
}
});
//StringBuffer sb = new StringBuffer();
String ans = "";
for(int i = 0; i < len; i++){
//sb.append(st[i]);
ans += inter[i];
}
//return sb.toString();
return ans;
}
}