How to copy (the contents of) ArrayList to a String array[]?

Before asking this question i have already checked this forum for question similar to this and have concluded that the only method to be able to copy an ArrayList into a String Array is to iterate through the ArrayList and then use the get method but i'm unable to do so?

Line 1: This statement throws error for incompatible types required String Found: Object so if i typecast it to String as dump[ia]=(String) list.get(ia); it throws a runtime ClassCastException error with the following stack trace

Thanks all, but my mistake here, i should have posted the whole code here.
Basically its the same program that i have been working on. It will read a text file, the text file contents are than populated into an ArrayList so as to find unique word occurences and finally write them out in an XML format. Koushik Ghosh helped me out with the xml part but i wanted to better the code, one of the tasks i wanted to do was to write a valid xml file, so somewhere down this path i found that my original ArrayList would not work for my XML file and that i needed to convert it into a String Array.

However, I'm not sure why you think you need to convert the contents to an array at all.

So Winston that's the story so far.

By the way, Winston i tried what you suggested and im still getting that ClassCastException error

Before asking this question i have already checked this forum for question similar to this and have concluded that the only method to be able to copy an ArrayList into a String Array is to iterate through the ArrayList and then use the get method but i'm unable to do so?

Just a second here, I missed this one before. you said // Here list is an ArrayList of type <String, Integer> An ArrayList can not have <String, Integer> , it can have only one.

Ashish Dutt wrote:So the updated code with kind corrections from Winston and Koushik clarifying it for me is as follows, but like i posted earlier, this code is now throwing NullPointerException at line 41

Why is 'list' defined as static? I'm not sure if that's the problem, but it's sure as hell NOT a good idea.

Ashish Dutt wrote:So the updated code with kind corrections from Winston and Koushik clarifying it for me is as follows, but like i posted earlier, this code is now throwing NullPointerException at line 41

Why is 'list' defined as static? I'm not sure if that's the problem, but it's sure as hell NOT a good idea.

Ashish, you have declared list as a global variable right? So here you don't need to return the list. modify the return type as void. When you modify list in the method the global variable (list) is modified. When you access the list from another method you get the modified list.

Ashish Dutt wrote:So the updated code with kind corrections from Winston and Koushik clarifying it for me is as follows, but like i posted earlier, this code is now throwing NullPointerException at line 41

here you have declared list 2 times. 1st time as a global variable; 2nd time in the constructor.
it should be somewhat like this

Koushik Ghosh wrote:here you don't need to return the list. modify the return type as void. When you modify list in the method the global variable (list) is modified. When you access the list from another method you get the modified list.

I hate to say, but I totally disagree with that suggestion.
1. Global variables are just plain bad.
2. If you're going to design it that way, why bother to have an object at all? Just make the whole darn thing static.
3. (and this, admittedly, is just a personal bias) I hate methods that return void.

@Ashish: But you're programming now, and there are many ways to do things.

My advice: Try to think about this from the point of view of a user of your class. How easy is it to use?

Sorry Koushik, please disregard this previous post where i wanted to know about list being declared twice.
it seems that both of us were busy writing our responses .. let me work on it and i will let you know.

Ashish, I'll tell you where you instantiated list variable twice.
See this is your code.

Ashish Dutt wrote:So the updated code with kind corrections from Winston and Koushik clarifying it for me is as follows, but like i posted earlier, this code is now throwing NullPointerException at line 41

The first time when you write this

it's actually creating a default global variable.
Next in the constructor when you write
it's again a declaration.
I've just changed it to

Koushik, Thanks a lot for your detailed explanation And updating the code.
But you will hate to know that when i ran the above code it failed with the same ClassCastException.
The only thing that i changed in it was the path where i assigned it a text file
This is the output of the code that you gave

Ashish Dutt wrote:But you will hate to know that when i ran the above code it failed with the same ClassCastException.

You haven't been reading your replies. If you look back, you'll see that I said you need to use toArray(T[] array) - and I even showed you how to do it. toArray() returns an Object[]; and in the case of ArrayList, it really IS an Object[].

Ashish Dutt wrote:But you will hate to know that when i ran the above code it failed with the same ClassCastException.

You haven't been reading your replies. If you look back, you'll see that I said you need to use toArray(T[] array) - and I even showed you how to do it. toArray() returns an Object[]; and in the case of ArrayList, it really IS an Object[].

Winston

Awesome, I carefully re-read your previous post again and rectified the problem
This call's for a standing ovation, Thank you very much Winston. Cheers.
Now, just do me one more favour, can you please explain to me what is the meaning of this sentence from your post

Then you can get a string array by using toArray(String[]), viz:
String[] dump = list.toArray(new String[0](new String[0]));

I cannot understand why you put a index 0 as String [0]?
Please do help explain this.
In fact i had read this post of yours earlier but since i could not understand the above underlined statement i just overlooked it.
Eagerly awaiting your reply.

Ashish Dutt wrote:Awesome, I carefully re-read your previous post again and rectified the problem
This call's for a standing ovation, Thank you very much Winston. Cheers.

You're most welcome.

Now, just do me one more favour, can you please explain to me what is the meaning of this sentence from your post

Then you can get a string array by using toArray(String[]), viz:
String[] dump = list.toArray(new String[0](new String[0]));

You read wrong. I said:
String[] dump = list.toArray( new String[0] ); and I could also have written it as:
String[] dump = list.toArray( new String[] {} );

So, break it down. What exactly does that statement do? (Hint: it has nothing to do with indexes).
Now read the documentation for toArray(T[]) again, and see if you can't work out why you might write it that way.

If you get really stuck, I'll help you out; but it'll be much better (and much more satisfying) if you work it out for yourself.

Thanks a ton, Winston.
I read the documentation on .toArray(T[]) .toArray() and .toArray(T[]) and deduced the following
the .toArray() method comes in two flavours;
the first one as public Object[] toArray() and the second one as public <T> T[] toArray(T[] a) so basically its an overloaded method.
Now the more important part is that in my previous code i was using which was throwing ClassCastException. The reason why it was doing so i have understood now, because i was using the .toArray() method whereas i should have used Because i had wanted to convert the list to a String Array and this second method does exactly that.
And both these methods do the same thing,

This method acts as bridge between array-based and collection-based APIs. (from Oracle docs)

Ashish Dutt wrote:There you caught me red handed.... :-( Now this answer is a fluke. Maybe because we want the String [] dump array to resize itself automatically according to the size of the ArrayList... ?

Correct. Again, it's in the docs that if the supplied array isn't big enough, it will be resized, so a 0-length array will always be resized unless the list itself is empty.

You could also do:
String[] dump = list.toArray( new String[list.size()] ); which is actually slightly quicker.

Thanks again Winston, for leveraging confidence in my otherwise skeptical cloud of thoughts. Much appreciated towards your constant endeavors..
I just wish i had joined Java Ranch earlier, i could have learnt so much more about Java.