現在正在使用Java來處理此項目一段時間。建議我爲我的程序使用鏈接列表或數組列表,這非常合理。然而,教授說我們必須製作和使用我們自己的鏈接列表利用節點。儘管在課堂上進行了一些研究和問詢,但與Nodes合作讓我感到非常困惑。我確信這是一件簡單的事情,但我現在處於完全失落狀態。這是列表存儲的類(我認爲)。它被稱爲飛機,因爲我們正在創建一個列表來存儲多架飛機和一些與它們相關的細節(飛行名稱,速度,高度,飛機類型)。我有一個用戶與之交互的主類(未列出) - 我已經很熟悉這個類。僅使用節點創建Java鏈接列表的幫助
package airTraffic;
public class Aircraft {
public static String name;
public static String type;
public static int speed;
public static int alt;
Aircraft nextCraft;
public Aircraft (String n, String t, int s, int a) {
name = n;
type = t;
speed = s;
alt = a;
}
public Aircraft() {
}
public static void setName(String n) {
name = n;
}
public static String getName (String lookUp) {
return name;
}
public static void removeName() {
//remove the flight - not sure what to place here
}
public static void setType (String t) {
type = t;
}
public static String getType() {
return type;
}
public static void setSpeed (int s) {
speed = s;
}
public static int getSpeed() {
return speed;
}
public static void setAlt(int a) {
alt = a;
}
public static int getAlt() {
return alt;
}
public Aircraft next = null;
//auto generated method from ATControl
public static void add(String s) {
}
//auto generated methods from ATControl - what goes here???
public static void remove() {
}
public Object getNext() {
// TODO Auto-generated method stub
return null;
}
public void setNext(Object next2) {
// TODO Auto-generated method stub
}
}
下面,我有我認爲是創建和存儲節點的類。這是我非常困惑,並認爲我錯了。我不知道如何調用節點來實際添加和存儲數據。我還需要能夠獲得的節點(通過飛行名),並刪除該節點(通過飛行名)
package airTraffic;
import java.util.*;
import airTraffic.Aircraft;
public class ATControl {
static Main m = new Main();
Aircraft aircraft = new Aircraft();
//declare node names for list
public static Aircraft head = new Aircraft();
public static Aircraft tail = new Aircraft();
// stores data
private static final int INITIAL_ALLOCATION = 20;
private static int size = INITIAL_ALLOCATION;
// tells list to add nodes
public static void Nodes (String s, int n) {
n = size;
head.next = tail;
tail.next = tail;
Aircraft temp = head;
for (int i= 0; i < size; ++i) {
temp.next = new Aircraft();
temp = temp.next;
}
temp.next = tail;
}
public static void addNodes (int n) {
n = size;
Aircraft temp = new Aircraft();
Aircraft current = head;
for (int i = 1; i < n && current.getNext() != null; i++) {
current = (Aircraft) current.getNext();
temp.setNext(current.getNext());
current.setNext (temp);
size++;
}
}
//add plane and details
public static void addToList (Scanner in) {
// should add new aircraft to new node on linked list
System.out.printf("Enter flight number: ");
String add = in.next();
Aircraft.setName (add);
ATControl.addNodes (Integer.parseInt(add));
//type of plane
System.out.printf("Enter type of plane: ");
String plane = in.next();
Aircraft.setType (plane);
//plane speed
System.out.printf("Enter current speed: ");
int speed = in.nextInt();
Aircraft.setSpeed (speed);
ATControl.addNodes (Integer.parseInt(add));
//add Altitude
System.out.printf("Enter current altitude: ");
int alt = in.nextInt();
Aircraft.setAlt(alt);
ATControl.addNodes (Integer.parseInt(add)); // I am fairly certain this is wrong
}
//show flight
public static void showFlight (Scanner in) {
System.out.printf("Enter flight number for details: ");
String lookUp = in.next();
Aircraft.getName(lookUp);
}
// display all flights
public static void displayAll (Scanner in) {
System.out.printf("All flights: ");
}
//remove flight
public static void removeFlight (Scanner in) {
System.out.printf("Enter flight number to be removed: ");
}
}
好答案.. +1 – 2014-02-24 09:16:29