如何使用链表中的对象
How to use the object from a Linked List
我正在使用链表进行作业,但我对某些事情感到困惑。我们必须为电话簿创建另一个 class,它接收姓名和号码,然后将其存储在链表中,我已经弄清楚了其中的大部分内容。直到我开始尝试从链表中取出 person 对象,这样我就可以 运行 我为 person 对象编写的方法,比如 getter 和 setter 等等。但是当我把它从链表中拉回来的时候,它只是一个对象,而不是我放入的人,请问如何把这个对象拉出来,这样我才能把它当作我放入的对象。
谢谢
好的,很抱歉将其添加到评论中。
这是我到目前为止尝试过的测试,
人 class 有一个打印方法,但我忘了用
分配类型
public static void main(String[] args) {
LinkedList phonedir = new LinkedList();
person one = new person("John", "Doe", "1234567890");
phonedir.add(one);
Object two = phonedir.get(0);
two.print();
}
你应该使用LinkedList<person>
这样编译器就知道你得到的是一个person
.
public static void main(String[] args) {
LinkedList<person> phonedir = new LinkedList<person>();
person one = new person("John", "Doe", "1234567890");
phonedir.add(one);
person two = phonedir.get(0);
two.print();
}
另外,应该是Person
。 Class Java 中的名称应以大写字母开头!
public class SaveRetrieveObjects {
List<Item> objs = new LinkedList<Item>();
File fileName = new File("objects.sav");
public SaveRetrieveObjects() {
createItems();
showItems();
serializeObjs(objs, fileName);
objs = deserializeObjs(objs, fileName);
showItems();
System.out.println("Vale!");
}
private void createItems() {
Item oneObject = new Item();
oneObject.setId("Citric-45");
oneObject.setDesc("orange");
oneObject.setCost(12345.67);
oneObject.setQty(67);
oneObject.setBool(true);
oneObject.setColor(Color.orange);
objs.add(oneObject);
oneObject = new Item();
oneObject.setId("Mobil-12");
oneObject.setDesc("car");
oneObject.setCost(67890.12);
oneObject.setQty(2);
oneObject.setBool(true);
oneObject.setColor(new Color(1, 2, 3));
objs.add(oneObject);
}
private void showItems() {
Item itm;
for (int j = 0; j < objs.size(); j++) {
itm = objs.get(j);
System.out.println("Item " + (j + 1) + " Contents:");
System.out.print("\t" + itm.getId());
System.out.print("\t" + itm.getDesc());
System.out.print("\t" + itm.getCost());
System.out.print("\t\t" + itm.getQty());
System.out.println("\t" + itm.isBool());
System.out.println("\t" + itm.getColor());
}
System.out.println("----------------------------------------------------");
}
private void serializeObjs(List<Item> objs, File file) {
System.out.println("Serialization process started");
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(objs);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("\nSerialization ended successfully");
System.out.println("Checkout your specified output file");
System.out.println("-----------------------------------");
}
@SuppressWarnings("unchecked")
private List<Item> deserializeObjs(List<Item> objs, File file) {
objs.clear();
System.out.println("Deserialization process started");
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
objs.addAll((Collection<? extends Item>) ois.readObject());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
fis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("Deserialization process ended");
return objs;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SaveRetrieveObjects();
}
});
}
}
class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String desc;
private double cost;
private int qty;
private boolean bool;
private Color color;
public Item() {
id = "";
desc = "";
cost = 0;
qty = 0;
bool = false;
color = new Color(0, 0, 0);
}
public Item(String id, String desc, double cost, int qty, boolean bool, Color color) {
this.id = id;
this.desc = desc;
this.cost = cost;
this.qty = qty;
this.bool = bool;
this.color = color;
}
public String getId() {
return id;
}
public void setId(String itemID) {
this.id = itemID;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getQty() {
return qty;
}
public void setQty(int quantity) {
this.qty = quantity;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
这是 "How to use Objects from a Linked List" 上的完整示例。
在 LinkedList 或 ArrayList 中使用对象:
步骤:
* -Create Class Item implements Serializable.
* - 创建项目对象。
* - 将它们添加到 ArrayList 或 LinkedList。这些,ArrayList 或 LinkedList,做
不需要可序列化!只有项目 class 需要实施
可序列化。
* - 使用流在所选文件中写入和检索对象。
希望这对其他人有所帮助。淡水河谷!
我正在使用链表进行作业,但我对某些事情感到困惑。我们必须为电话簿创建另一个 class,它接收姓名和号码,然后将其存储在链表中,我已经弄清楚了其中的大部分内容。直到我开始尝试从链表中取出 person 对象,这样我就可以 运行 我为 person 对象编写的方法,比如 getter 和 setter 等等。但是当我把它从链表中拉回来的时候,它只是一个对象,而不是我放入的人,请问如何把这个对象拉出来,这样我才能把它当作我放入的对象。 谢谢
好的,很抱歉将其添加到评论中。 这是我到目前为止尝试过的测试, 人 class 有一个打印方法,但我忘了用
分配类型public static void main(String[] args) {
LinkedList phonedir = new LinkedList();
person one = new person("John", "Doe", "1234567890");
phonedir.add(one);
Object two = phonedir.get(0);
two.print();
}
你应该使用LinkedList<person>
这样编译器就知道你得到的是一个person
.
public static void main(String[] args) {
LinkedList<person> phonedir = new LinkedList<person>();
person one = new person("John", "Doe", "1234567890");
phonedir.add(one);
person two = phonedir.get(0);
two.print();
}
另外,应该是Person
。 Class Java 中的名称应以大写字母开头!
public class SaveRetrieveObjects {
List<Item> objs = new LinkedList<Item>();
File fileName = new File("objects.sav");
public SaveRetrieveObjects() {
createItems();
showItems();
serializeObjs(objs, fileName);
objs = deserializeObjs(objs, fileName);
showItems();
System.out.println("Vale!");
}
private void createItems() {
Item oneObject = new Item();
oneObject.setId("Citric-45");
oneObject.setDesc("orange");
oneObject.setCost(12345.67);
oneObject.setQty(67);
oneObject.setBool(true);
oneObject.setColor(Color.orange);
objs.add(oneObject);
oneObject = new Item();
oneObject.setId("Mobil-12");
oneObject.setDesc("car");
oneObject.setCost(67890.12);
oneObject.setQty(2);
oneObject.setBool(true);
oneObject.setColor(new Color(1, 2, 3));
objs.add(oneObject);
}
private void showItems() {
Item itm;
for (int j = 0; j < objs.size(); j++) {
itm = objs.get(j);
System.out.println("Item " + (j + 1) + " Contents:");
System.out.print("\t" + itm.getId());
System.out.print("\t" + itm.getDesc());
System.out.print("\t" + itm.getCost());
System.out.print("\t\t" + itm.getQty());
System.out.println("\t" + itm.isBool());
System.out.println("\t" + itm.getColor());
}
System.out.println("----------------------------------------------------");
}
private void serializeObjs(List<Item> objs, File file) {
System.out.println("Serialization process started");
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(objs);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("\nSerialization ended successfully");
System.out.println("Checkout your specified output file");
System.out.println("-----------------------------------");
}
@SuppressWarnings("unchecked")
private List<Item> deserializeObjs(List<Item> objs, File file) {
objs.clear();
System.out.println("Deserialization process started");
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
objs.addAll((Collection<? extends Item>) ois.readObject());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
fis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("Deserialization process ended");
return objs;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SaveRetrieveObjects();
}
});
}
}
class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String desc;
private double cost;
private int qty;
private boolean bool;
private Color color;
public Item() {
id = "";
desc = "";
cost = 0;
qty = 0;
bool = false;
color = new Color(0, 0, 0);
}
public Item(String id, String desc, double cost, int qty, boolean bool, Color color) {
this.id = id;
this.desc = desc;
this.cost = cost;
this.qty = qty;
this.bool = bool;
this.color = color;
}
public String getId() {
return id;
}
public void setId(String itemID) {
this.id = itemID;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getQty() {
return qty;
}
public void setQty(int quantity) {
this.qty = quantity;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
这是 "How to use Objects from a Linked List" 上的完整示例。
在 LinkedList 或 ArrayList 中使用对象:
步骤:
* -Create Class Item implements Serializable.
* - 创建项目对象。
* - 将它们添加到 ArrayList 或 LinkedList。这些,ArrayList 或 LinkedList,做
不需要可序列化!只有项目 class 需要实施
可序列化。
* - 使用流在所选文件中写入和检索对象。
希望这对其他人有所帮助。淡水河谷!