471,330 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

2 questions about array lists

Hello,

I have 2 questions about using array lists.

1. I have a method which returns an arraylist. I'd like to put the
contents of what is returned into another arraylist:

ArrayList al = new ArrayList();
al.AddRange(obj.getArrayList());

The two lines above fail miserably.

2. Can the contents of an arraylist of strings populate a richtextbox
without iterating through the collection? I've seen this: rtb.Text
+= myList[i].toString() but is there a built-in method which would do
the same thing?

Thanks,

--Chris
Jun 27 '08 #1
9 1214
On Apr 24, 11:04*am, Adam Sandler <cor...@excite.comwrote:
Hello,

I have 2 questions about using array lists.

1. I have a method which returns an arraylist. *I'd like to put the
contents of what is returned into another arraylist:
Why?
You mean, you want to copy it?

Have you tried ArrayList.Clone?

Also AddRange should work:
From MSDN:
ArrayList.AddRange Method:
public virtual void AddRange (
ICollection c
)

public class ArrayList : IList, ICollection, IEnumerable,
ICloneable

So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work
Jun 27 '08 #2
On Apr 24, 9:18*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work
It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.
Jun 27 '08 #3
JS
ArrayList al = new ArrayList();
al.AddRange(obj.getArrayList());

The two lines above fail miserably.
What does 'fail miserably' mean? Failed to compile/throws an
exception/unexpected behavior? If it is an exception, catch it and
see if this helps you figure out the problem.
These 2 lines look fine assuming that obj is not null and
getArrayList() returns a non-null array list.
>
2. *Can the contents of an arraylist of strings populate a richtextbox
without iterating through the collection? *I've seen this: *rtb.Text
+= myList[i].toString() but is there a built-in method which would do
the same thing?
I am not aware of a built-in method, but I would do it something like
this:
StringBuilder sb = new StringBuilder();
foreach (string s in myList) sb.Append(s);
rtb.Text = sb.ToString();
Jun 27 '08 #4
Adam Sandler wrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work

It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.
Whatever is being returned from your obj.getArrayList() function (which we have
seen no definition for) must not be an actual ArrayList or fails to implement
ICollection at any rate.

The following code compiles and executes just fine for me:

static void Main()
{
ArrayList other = new ArrayList();
other.AddRange(new object[] { "One", 2, "Three", "Four", 5 });

ArrayList al = new ArrayList();
al.AddRange(other);
}

Chris.
Jun 27 '08 #5
On Apr 24, 11:23*am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18*am, "Ignacio Machin ( .NET/ C# MVP )"

<ignacio.mac...@gmail.comwrote:
So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work

It doesn't. *I've typed the code exactly as it appears in the IDE. *I
'm aware that arraylists implements icollection; that is why I used
used AddRange.
ArrayList ar = new ArrayList();
ar.Add(1); ar.Add(2); ar.Add(3);
ArrayList ar2 = new ArrayList();
ar2.AddRange(ar);
Console.WriteLine(ar2.Count);

I just tested it and worked fine
Jun 27 '08 #6
On Apr 24, 12:27 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Apr 24, 11:23 am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work
It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.

ArrayList ar = new ArrayList();
ar.Add(1); ar.Add(2); ar.Add(3);
ArrayList ar2 = new ArrayList();
ar2.AddRange(ar);
Console.WriteLine(ar2.Count);

I just tested it and worked fine
Thanks everyone for their help so far:

Here's everything I have:

In one class here is this:

private ArrayList al = new ArrayList();

public void setWordList(String s)
{
al.Add(s);
}

public ArrayList getWordList()
{
return al;
}

The contents of the arraylist are read from a text file:

public void ReadFromFile(string filename)
{
StreamReader sr;
string s;

sr = File.OpenText(filename);
s = SR.ReadLine();

WordLists wl = new WordLists();

while (s != null)
{
wl.setWordList(a);
s = sr.ReadLine();
}
sr.Close();
}

Yes, I've verified there are items in the arraylist prior to
continuing. Eventually, and based upon what form the user displays, I
want to put the arraylist contents into a richtextbox. So from the
form's constructor, I call this:

public ListEditor()
{
WordLists wl = new WordLists();
ArrayList al = new ArrayList();

al.AddRange(wl.getWordList);
}

The line which reads, al.AddRange(wl.getWordList) has two errors:

Error 1 The best overloaded method match for
'System.Collections.ArrayList.AddRange(System.Coll ections.ICollection)'
has some invalid arguments
Error 2 Argument '1': cannot convert from 'method group' to
'System.Collections.ICollection'

Again, thanks to everyone for their help thus far
Jun 27 '08 #7
On 2008-04-24, Adam Sandler <co****@excite.comwrote:
On Apr 24, 12:27 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>On Apr 24, 11:23 am, Adam Sandler <cor...@excite.comwrote:
On Apr 24, 9:18 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
So AddRange expect an ICollection and ArrayList implements
ICollection, so it should work
It doesn't. I've typed the code exactly as it appears in the IDE. I
'm aware that arraylists implements icollection; that is why I used
used AddRange.

ArrayList ar = new ArrayList();
ar.Add(1); ar.Add(2); ar.Add(3);
ArrayList ar2 = new ArrayList();
ar2.AddRange(ar);
Console.WriteLine(ar2.Count);

I just tested it and worked fine

Thanks everyone for their help so far:

Here's everything I have:

In one class here is this:

private ArrayList al = new ArrayList();

public void setWordList(String s)
{
al.Add(s);
}

public ArrayList getWordList()
{
return al;
}

The contents of the arraylist are read from a text file:

public void ReadFromFile(string filename)
{
StreamReader sr;
string s;

sr = File.OpenText(filename);
s = SR.ReadLine();

WordLists wl = new WordLists();

while (s != null)
{
wl.setWordList(a);
s = sr.ReadLine();
}
sr.Close();
}

Yes, I've verified there are items in the arraylist prior to
continuing. Eventually, and based upon what form the user displays, I
want to put the arraylist contents into a richtextbox. So from the
form's constructor, I call this:

public ListEditor()
{
WordLists wl = new WordLists();
ArrayList al = new ArrayList();

al.AddRange(wl.getWordList);
}

The line which reads, al.AddRange(wl.getWordList) has two errors:
You don't have any parens after your call... The method takes an object
of ICollection, but the compiler is going to try and infer this as a
delegate.

On a side note... If these are all strings - you might want to change
your type to List<stringinstead of an ArrayList.

--
Tom Shelton
Jun 27 '08 #8
On Apr 24, 3:27 pm, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
You don't have any parens after your call...
ARRRRGGGHHH! I hate when that happens. Thanks to everyone for their
help!!!
Jun 27 '08 #9
Adam Sandler wrote:
On Apr 24, 3:27 pm, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote:
>You don't have any parens after your call...

ARRRRGGGHHH! I hate when that happens. Thanks to everyone for their
help!!!
That's what "cannot convert from 'method group'" means almost 100% of the
time.
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Colin J. Williams | last post: by
2 posts views Thread by bearophile | last post: by
1 post views Thread by Aaron Lovi | last post: by
27 posts views Thread by Jatinder | last post: by
7 posts views Thread by alternativa | last post: by
5 posts views Thread by bruce | last post: by
5 posts views Thread by Tobiah | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.