现在正在使用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