當前位置:編程學習大全網 - 源碼下載 - 求JAVA小程序設計壹個鏈表結點類LinkNode,此類可以存放int、long

求JAVA小程序設計壹個鏈表結點類LinkNode,此類可以存放int、long

//幫樓主改好了。有三個類。分別放到對應的文件裏。文件名要和類名相同。註意大小寫。如LinkNode.java

//第壹個類

public class Content {

private int key;

private int name;

//int、long、float、double、byte、short、String、StringBuffer

public Content(int key, int name) {

this.key = key;

this.name = name;

}

public Content(int key, long name) {

this.key = key;

this.name = (int)name;

}

public Content(int key, double name) {

this.key = key;

this.name = (int)name;

}

public Content(int key, byte name) {

this.key = key;

this.name = (int)name;

}

public Content(int key, short name) {

this.key = key;

this.name = (int)name;

}

public Content(int key, String name) {

this.key = key;

this.name = Integer.parseInt(name);

}

public long getKey() {

return key;

}

public void setKey(int key) {

this.key = key;

}

public int getName() {

return name;

}

public void setName(int name) {

this.name = name;

}

}

//第二個類

import java.util.Comparator;

public class ContentComparator implements Comparator {

public int compare(Object o1, Object o2) {

// TODO Auto-generated method stub

Content c1 = (Content) o1;

Content c2 = (Content) o2;

if (c1.getName() > c2.getName()) {

return 1;

} else {

if (c1.getName() == c2.getName()) {

return 0;

} else {

return -1;

}

}

}

}

//第三個類

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class LinkNode {

public static void main(String[] args) {

// TODO Auto-generated method stub

List list = new ArrayList();

for (int i = 0 ; i < 10; i++){

int a = (int) (Math.random() * (100-1)+1);

list.add(new Content(i,a));

}

ContentComparator comp = new ContentComparator();

Collections.sort(list,comp);

Content content;

for(int i = 0; i < list.size(); i++){

content = (Content)list.get(i);

System.out.println(" Random " + content.getName());

}

}

}

  • 上一篇:修正概算指標計算公式
  • 下一篇:遼寧警察學院專業介紹
  • copyright 2024編程學習大全網