小编She*_*kie的帖子

序列化HashTable,Java

我以前从未使用过序列化.我想我没事,除了我的"Q"案例切换的最后一部分.

public class Test{

public static void main(String args[]){

    Store store = new Store();

    FileOutputStream fos;
    ObjectOutputStream oos = null;

    try{

        fos = new FileOutputStream(new File("table.obj"));
        oos = new ObjectOutputStream(fos);

    }catch(IOException e1){

        e1.printStackTrace();

    }
Run Code Online (Sandbox Code Playgroud)

这继续包含一堆更多的代码,但我认为真正重要的是我的"Q"案例......

case "Q":

            System.out.println("Good-Bye!");

            try{

                oos.writeObject(store);
                oos.flush();
                oos.close();

            }catch(IOException e){

                e.printStackTrace();

            }

            System.exit(0);

            break;
Run Code Online (Sandbox Code Playgroud)

当我尝试将所有数据保存到我的.obj文件并关闭流并退出我的程序时,我得到所有这些错误......

java.io.NotSerializableException:位于java.io.ObjectOutputStream.writeObject(未知源)的java.io.ObjectOutputStream.writeObject(未知源)中的项目,位于sun.reflect.NativeMethodAccessorImpl的java.util.Hashtable.writeObject(未知源).在java.io.ObjectStreamClass的java.lang.reflect.Method.invoke(未知来源)的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)的sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)中的invoke0(本机方法). java.io.ObjectOutputStream.defaultWriteFields上的java.io.ObjectOutputStream.writeObject0(未知来源)java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)java上的invoWriteObject(未知来源) java.io.ObjectOutputStream.writeObject(Unknown Source)at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)at java.io.ObjectOutputStream.writeObject0(Unknown Source)at java.io.ObjectOutputStream.writeObject0(Unknown Source)at java.io.ObjectOutputStream.writeObject(Unkno)wn Source)在Test.main(Test.java:143)

我不确定这些错误大多数意味着什么,或者为什么我会得到它们,甚至不知道如何修复它们.谁能帮我?

编辑:商店类

import java.io.Serializable;
import java.util.Hashtable;

 public class Store implements Serializable{

Hashtable<String, Item> stockedItems = new Hashtable<String, Item>();

public …
Run Code Online (Sandbox Code Playgroud)

java serialization

6
推荐指数
1
解决办法
5628
查看次数

对象数组实例的深度复制,Java

我在我的 main 中制作了一个对象Recipe recipeOne = new Recipe("Pepperoni Pizza");

该对象是此处定义和构造的对象数组的实例!

public class Recipe implements Cloneable{

String Name;

final int INGREDIENT_ARRAY_MAX = 10;

Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX];

public Recipe(String name){

    Name = name;

}
Run Code Online (Sandbox Code Playgroud)

所以我想用这条线制作这个对象的深层副本Recipe ressippi = (Recipe) recipe.clone();,它把我发送到这里!

public Object clone(){

    Recipe cloneRec = new Recipe(Name);

    return cloneRec;

}
Run Code Online (Sandbox Code Playgroud)

我知道这目前是一个浅拷贝,因为该方法只传递引用,所以如果我尝试对我的新对象(它是recipeOne 的克隆)进行名称更改...它将更改它们的两个名称。显然我不希望这样,我对此很迷茫,有人可以帮忙吗?

编辑:@Rohit Jain

我的 Recipe 类和 Ingredient 类(菜谱数组保存的对象)都有 toString 方法,并且 Recipe 调用配料,以便以漂亮的小格式将其全部打印出来。当我在我的“recipeOne”对象(称为意大利辣香肠披萨的那个)上调用它时,我得到“意大利辣香肠披萨:1.0 磅面团,8.0 盎司酱汁,10.0 盎司奶酪”

然后我继续创建 ressippi 对象并将其设置为recipeOne 的克隆,所以从这里开始一切都很好...然后我将 ressippi 的名称更改为“Pineapple Pizza”,打印效果很好,但它不打印recipeOne 的 3 个成分对象存储,这是它应该做的!

java arrays object deep-copy

5
推荐指数
1
解决办法
6516
查看次数

创建对对象的引用

我在这里有这个对象:

public class ItemInfo {

    String productName;
    String rfidTagNumber;
    String originalLocal;
    String currentLocal;
    double productPrice;

    public ItemInfo(String name, String tag, String origLoc, String curLoc, double price) {
        productName = name;
        rfidTagNumber = tag;
        originalLocal = origLoc;
        currentLocal = curLoc;
        productPrice = price;
    }
Run Code Online (Sandbox Code Playgroud)

我需要ItemInfo在这里创建并引用一个对象:

public class ItemInfoNode {

    ItemInfoNode next;
    ItemInfoNode prev;

    public ItemInfoNode(){

    }

    public void setInfo(ItemInfo info) {

    }

    public ItemInfo getInfo(){
        return null;    
    }

    public void setNext(ItemInfoNode node) {

    }

    public void setPrev(ItemInfoNode node) { …
Run Code Online (Sandbox Code Playgroud)

java reference object

2
推荐指数
1
解决办法
2万
查看次数

在C中使用块

我有这个代码

#include <stdio.h>
#include <stdlib.h>

int main(){
int a = 5;
{
    int b = 6;
}
printf("%d %d", a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试图看看如何使用块会影响这个但程序不起作用.说b是未宣布的,这是我给出的例子.谁知道什么是错的?或者有可能这是假设抛出我并且错误,因为当printf不在那里时int块被声明并初始化在块中?

c block

2
推荐指数
1
解决办法
117
查看次数

使用VC++ Express在C中创建文本文件

好吧这是一个奇怪的问题,但我不知道为什么这不起作用......

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME 15
#define MAX_SUBSEC 3
#define N 128

struct student{

int term;
int id;
char lastname[MAX_NAME];
char firstname[MAX_NAME];
char subjectnam[MAX_SUBSEC];
int subject;
int catalog;
char section[MAX_SUBSEC];

}students[10];

int main(){

int i;
char poop[10];
char fname[128];

printf("Enter the name of the text file: ");
scanf("%123s",fname);

strcat(fname,".txt");

FILE *inputf;
inputf = fopen(fname,"w");

if (inputf == NULL){

     printf("I couldn't open results.dat for writing.\n");
     exit(0);

  }

printf("Enter first name: "); scanf("%s", poop);

fprintf(inputf, "%s\n", poop);

for …
Run Code Online (Sandbox Code Playgroud)

c file-io visual-studio

1
推荐指数
1
解决办法
2377
查看次数

编写类是Vector的子类,java

只是希望我能找人告诉我我这样做是对的,因为我之前从未写过这样的东西.所以我必须编写一个名为ProcessQueue的类,它是Vector的子类,以及一个定义空队列的构造函数.Vector也将保存"对象"类型的项目.所以这里......

public class ProcessQueue<Vector>{

ProcessQueue(){}


}
Run Code Online (Sandbox Code Playgroud)

java constructor vector

-2
推荐指数
2
解决办法
2631
查看次数