遍历
链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器,我们使用这个迭代器来遍历
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
public static void main(String []args){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task task=new Task();
task.solve(1,in,out);
out.close();
}
static class Task{
List<Integer> list=new ArrayList<>();
int n,num;
public void solve(int testnumber,InputReader in,PrintWriter out){
n=in.nextInt();
for(int i=0;i<n;i++){
num=in.nextInt();
list.add(num);
}
Iterator<Integer> iter=list.iterator();
while(iter.hasNext()){
int te=iter.next();
out.print(te+" ");
}
out.print("");
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
排序和查找
如果链表中的数据是实现了Comparable接口的实例,如String,Integer对象,那么java.util包中的Collections类中的sort
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
public static void main(String []args){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task task=new Task();
task.solve(1,in);
out.close();
}
static class People implements Comparable<People>{
int height,weight;
public People(int h,int w){
height=h;
weight=w;
}
public int compareTo(People b){
if(this.height==b.height){
return this.weight-b.weight;
}
return (this.height-b.height);
}
}
static class Task{
List<String> list1=new ArrayList<String> ();
List<People> list2=new ArrayList<People>();
int n1,n2;
String s;
int height,weight;
public void solve(int testnumber,InputReader in){
n1=in.nextInt();
for(int i=0;i<n1;i++){
s=in.next();
list1.add(s);
}
System.out.print("\n");
Collections.sort(list1);
Iterator<String> iter1=list1.iterator();
while(iter1.hasNext()){
System.out.print(iter1.next()+" ");
}
System.out.print("\n");
String key1;
key1=in.next();
int index=Collections.binarySearch(list1,key1);
if(index>=0){
System.out.println("链表中存在"+key1+"字符串");
} else {
System.out.println("链表中不存在");
}
n2=in.nextInt();
for(int i=0;i<n2;i++){
height=in.nextInt();
weight=in.nextInt();
list2.add(new People(height,weight));
}
Collections.sort(list2);
Iterator<People> iter2=list2.iterator();
while(iter2.hasNext()){
People p = iter2.next();
System.out.println("身高:"+p.height+"体重:"+p.weight);
}
index=Collections.binarySearch(list2,new People(170,80));
if(index>=0){
System.out.println("链表存在数据");
} else {
System.out.println("不存在");
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
整型
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
public static void main(String []args){
InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);
Task task=new Task();
task.solve(1,in);
}
static class Int implements Comparable<Int>{
int num;
public Int(int num){
this.num=num;
}
public int compareTo(Int b){
return (b.num-this.num);
}
}
static class Task{
int n,num;
List<Int> list=new ArrayList<Int>();
public void solve(int testnumber,InputReader in){
n=in.nextInt();
for(int i=0;i<n;i++){
num=in.nextInt();
list.add(new Int(num));
}
Collections.sort(list);
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).num+" ");
}
System.out.println("");
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}