package org.jhy.com;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
/*MyLinkedList mll = new MyLinkedList();
mll.add("1");
mll.add("2");
mll.add("3");
mll.add("4");
mll.add("5");
mll.add("6");
mll.add("7");
mll.add("8");
for(int i=0;i<mll.size();i++){
System.out.println(mll.get(i));
}*/
/*LinkedList<String> ll = new LinkedList<>();
ll.add("1");
ll.add("2");
ll.add("3");
ll.add("4");
ll.add("5");
ll.add("6");
ll.removeFirst();
ll.removeLast();
ll.remove(1);
for(int i=0;i<ll.size();i++){
System.out.println(ll.get(i));
}*/
//Set:无序、不可重复 List:有序、可重复
/*HashSet<String> set = new HashSet<String>();//数学集合
Scanner input = new Scanner(System.in);
String words = input.nextLine();
input.close();
String[] ws = words.split(" ");
for(String w:ws){
set.add(w);
}
System.out.printf("共有%d个单词\n",set.size());
for(String s:set){
System.out.print(s+" ");
}*/
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(1,"1"));
al.add(new Student(2,"2"));
al.add(new Student(3,"3"));
al.add(new Student(4,"4"));
al.add(new Student(5,"5"));
Student temp = new Student(1,"1");
/*Student s0 = al.get(0);
if(s0.equals(temp)){
System.out.println("OK");
}else{
System.out.println("No!");
}*/
if(!al.contains(temp)){//equals
al.add(temp);
}
for(Student s:al){
System.out.println(s.getId());
}
}
}
class Student {
public int id;
public String name;
public Student(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class MyLinkedList{//
private Node first = null;
void add(Object object){//增加节点
if(first==null){
first = new Node(object);
}else{
Node last = first;
while(last.next!=null){
last = last.next;
}
last.next = new Node(object);
}
}
int size(){
int count = 1;
Node last = first;
while(last.next!=null){
last = last.next;
count++;
}
return count;
}
Object get(int index){
if(index>size()){
return null;
}
Node last = first;
int count = 0;
while(count<index){
last = last.next;
count++;
}
return last.o;
}
private class Node{
Object o;
Node next = null;
public Node(Object o){
this.o = o;
}
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
/*MyLinkedList mll = new MyLinkedList();
mll.add("1");
mll.add("2");
mll.add("3");
mll.add("4");
mll.add("5");
mll.add("6");
mll.add("7");
mll.add("8");
for(int i=0;i<mll.size();i++){
System.out.println(mll.get(i));
}*/
/*LinkedList<String> ll = new LinkedList<>();
ll.add("1");
ll.add("2");
ll.add("3");
ll.add("4");
ll.add("5");
ll.add("6");
ll.removeFirst();
ll.removeLast();
ll.remove(1);
for(int i=0;i<ll.size();i++){
System.out.println(ll.get(i));
}*/
//Set:无序、不可重复 List:有序、可重复
/*HashSet<String> set = new HashSet<String>();//数学集合
Scanner input = new Scanner(System.in);
String words = input.nextLine();
input.close();
String[] ws = words.split(" ");
for(String w:ws){
set.add(w);
}
System.out.printf("共有%d个单词\n",set.size());
for(String s:set){
System.out.print(s+" ");
}*/
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(1,"1"));
al.add(new Student(2,"2"));
al.add(new Student(3,"3"));
al.add(new Student(4,"4"));
al.add(new Student(5,"5"));
Student temp = new Student(1,"1");
/*Student s0 = al.get(0);
if(s0.equals(temp)){
System.out.println("OK");
}else{
System.out.println("No!");
}*/
if(!al.contains(temp)){//equals
al.add(temp);
}
for(Student s:al){
System.out.println(s.getId());
}
}
}
class Student {
public int id;
public String name;
public Student(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class MyLinkedList{//
private Node first = null;
void add(Object object){//增加节点
if(first==null){
first = new Node(object);
}else{
Node last = first;
while(last.next!=null){
last = last.next;
}
last.next = new Node(object);
}
}
int size(){
int count = 1;
Node last = first;
while(last.next!=null){
last = last.next;
count++;
}
return count;
}
Object get(int index){
if(index>size()){
return null;
}
Node last = first;
int count = 0;
while(count<index){
last = last.next;
count++;
}
return last.o;
}
private class Node{
Object o;
Node next = null;
public Node(Object o){
this.o = o;
}
}
}