如何存储对象列表? [英] How do you store a list of Objects ?

查看:92
本文介绍了如何存储对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手..


我正在尝试列出目录中的所有文件,并将它们存储在aFile

对象中。当我遍历每个文件时,我会存储大小,名称,路径,日期

等...


如果对象是,我该如何存储它们产生的?我认为他们只需将
添加到ArrayList中,但这似乎无法正常工作。


例如:


每个文件{

myFile afile = new myFile();

afile.name = ...

。 ..

ArrayList.add(aFile);

}


但是这似乎在所有数组中存储了相同的对象插槽......


如何做到这一点?

I''m a newbie..

I''m trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would simply
be added to an ArrayList, but this doesn''t seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?

推荐答案

" Craig Lister" <我********* @ thelisters.co.uk>在留言中写道

news:43 *********************** @ news.zen.co.uk ...
"Craig Lister" <My*********@thelisters.co.uk> wrote in message
news:43***********************@news.zen.co.uk...
我是新手..

我正在尝试列出目录中的所有文件,并将它们存储在aFile
对象中。当我遍历每个文件时,我会存储大小,名称,路径,日期等等......

创建对象后如何存储它们?我以为它们会简单地添加到ArrayList中,但这似乎并没有起作用。

例如:

每个文件{
myFile afile = new myFile();
afile.name = ...
...
ArrayList.add(aFile);
}

应该怎么做?
I''m a newbie..

I''m trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would
simply be added to an ArrayList, but this doesn''t seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?




do


ArrayList a = new ArrayList();


然后


a.Add(。 ..);


您必须分配一个新的ArrayList来存储您的对象引用。如果您使用v2.0

,您可能会发现


List< T>在System.Collections.Generic更好


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk



do

ArrayList a = new ArrayList();

then

a.Add(...);

You have to allocate a new ArrayList to store your object references. If you
are using v2.0 you may find

List<T> in System.Collections.Generic better

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Craig Lister写道:
Craig Lister wrote:
我是新手..

我正在尝试列出目录中的所有文件,并将它们存储在aFile中<对象。当我遍历每个文件时,我会存储大小,名称,路径,日期等等......

创建对象后如何存储它们?我以为它们会简单地添加到ArrayList中,但这似乎并没有起作用。

例如:

每个文件{
myFile afile = new myFile();
afile.name = ...
...
ArrayList.add(aFile);
}

应该怎么做?
I''m a newbie..

I''m trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would simply
be added to an ArrayList, but this doesn''t seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?




嗯,我怀疑你的代码并不像上面那样。 (事实上​​,我是

确定它不是。)我的猜测是你的代码实际上创建了一个新的

" myFile"实例*一次*,然后多次设置它的名字。


然而,如果你能发布一个

短但完整的,那么最好不要猜测。演示问题的程序。请参阅
http://www.pobox.com/ ~sipet / csharp / complete.html 我的意思是

这个更详细。


另外,如果你能说你是否''使用.NET 1.1或2.0,这将是

帮助。


Jon



Well, I suspect your code isn''t quite like the above. (In fact, I''m
sure it''s not.) My guess is that your code actually creates a new
"myFile" instance *once*, then sets its name multiple times.

However, instead of guessing, it would be best if you could post a
short but complete program that demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this in more detail.

Also, if you could say whether you''re using .NET 1.1 or 2.0 that would
help.

Jon


谢谢。

好​​的,我正在使用VS 2005专业版(2.0)


现在看起来似乎有效......但是......这是最好的方法吗?

基本上,我在目录中扫描某种类型的文件。然后我在服务器上扫描一个目录

,并检查文件是否完全相同。如果他们是不同的b $ b,我将新版本复制到服务器....


帽子还没有完成!我只是想找到最好的开始方式。我的想法是为服务器上的每个文件创建一个对象。 (也许这不是一个好的计划,如果我们有20,000个文件?),然后用它来比较本地机器上的

实际文件。如果文件不同,请将本地

复制到服务器,更新对象并继续...


这里的代码:


私有类aFile //包含文件的类...

{


public string fileName =" " ;;


public long fileSize = 0;


public DateTime fileDateTime = System.DateTime.Now;


}


ArrayList fileList; //持有对象


private void getFileList(string theDir)//填充

对象的函数......


{


int myNum = 0;


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


{


myNum ++;


aFile myFile = new aFile();


myFile.fileName =" Test.exe";


myFile.fileSize = myNum;


myFile。 fileDateTime = System.DateTime.Now;


fileList.Add(myFile);


}


}


private void button1_Click(object sender,EventArgs e)

{

getFileList(edbxSource。文字);


for(int i = 0; i< fileList.Count - 1; i ++)


{


MessageBox.Show((fileList [i] as aFile).fileSize.ToString());


}


}


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Thanks.
OK, I''m using VS 2005 Pro (2.0)

It does seem to work now... But .. is this the best way to do this?
Basically, I scan a directory for files of a certain type. I then scan a dir
on a server, and check that the files are all the same. If they are
different, I copy the new version to the server....

None of hat is done yet! I''m just trying to find the best way to start. My
idea is to create an object for each file on the server. (Maybe this isn''t a
good plan, if we have 20,000 files?), and then use that to compare to the
actual file on the local machine. If the file is different, copy the local
to the server, update the object and continue...

Here''s code:

private class aFile // The class that holds the files...
{

public string fileName = "";

public long fileSize = 0;

public DateTime fileDateTime = System.DateTime.Now;

}

ArrayList fileList; // Holds the Objects

private void getFileList(string theDir) // Function that populates the
objects...

{

int myNum = 0;

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

{

myNum++;

aFile myFile = new aFile();

myFile.fileName = "Test.exe";

myFile.fileSize = myNum;

myFile.fileDateTime = System.DateTime.Now;

fileList.Add(myFile);

}

}

private void button1_Click(object sender, EventArgs e)

{
getFileList(edbxSource.Text);

for (int i = 0; i < fileList.Count - 1; i++)

{

MessageBox.Show((fileList[i] as aFile).fileSize.ToString());

}

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Craig Lister写道:
Craig Lister wrote:
我是新手..

我正在尝试列出目录中的所有文件,并将它们存储在aFile中对象。当我遍历每个文件时,我会存储大小,名称,路径,日期
等等......

创建对象后如何存储它们?我以为它们会简单地添加到ArrayList中,但这似乎并没有起作用。

例如:

file {
myFile afile = new myFile();
afile.name = ...
...
ArrayList.add(aFile);
}
应该怎么做?
I''m a newbie..

I''m trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path,
date
etc...

How do I store them once the object is created? I thought they would
simply
be added to an ArrayList, but this doesn''t seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?



,我怀疑你的代码并不像上面那样。 (事实上​​,我确定它不是。)我的猜测是你的代码实际上创建了一个新的
myFile。实例*一次*,然后多次设置其名称。

然而,如果您可以发布一个简短但完整的程序来证明问题,那么最好不要猜测。请参阅
http://www.pobox.com/ ~siget / csharp / complete.html 我的意思是
这个更详细。

另外,如果你可以说你是使用.NET 1.1还是这将有助于帮助。

Jon



Well, I suspect your code isn''t quite like the above. (In fact, I''m
sure it''s not.) My guess is that your code actually creates a new
"myFile" instance *once*, then sets its name multiple times.

However, instead of guessing, it would be best if you could post a
short but complete program that demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this in more detail.

Also, if you could say whether you''re using .NET 1.1 or 2.0 that would
help.

Jon



这篇关于如何存储对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆