• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

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


I also tried the toArray() but it gave the same above ClassCastException at runtime

So please tell me how do i then copy the elements of ArrayList into a String array?>
 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think that some piece is missing since the ArrayList only could be typed as ArrayList<E>
and you are stating that you have an array list like that ArrayList<String, Integer>.

The exception says that "java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String; " you are trying to cast a String to a String[].

The exception that you have posted doesn't match with the attached code.


Best regards,


 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get any exception when I run this code :
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:So please tell me how do i then copy the elements of ArrayList into a String array?>

Simple: Don't.

You're looking at the problem from the wrong end.

If the contents of your ArrayList are Strings, tell the compiler that by making your List a List<String>.
List<String> list = new ArrayList<String>();

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

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

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:By the way, Winston i tried what you suggested and im still getting that ClassCastException error

Because your 'list' variable is defined as an ArrayList, NOT an ArrayList<String> - and BTW, it should be a List<String> anyway.

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I mad the change.
Defined the ArrayList variable list as String but still the same error

Than in the constructor WordFinder intialised it as

However i could not understand this last part
and BTW, it should be a List<String> anyway.
Are you referring to here? the ArrayList variable name?
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's trying to tell you to instantiate the arraylist like this :

I'm not quite sure whether this is the root of the exeption, but try it anyway. If problem persist then let us know.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's trying to tell you to instantiate the arraylist like this :

I'm not quite sure whether this is the root of the exeption, but try it anyway. If problem persist then let us know.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Dear all,

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
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koushik Ghosh wrote:He's trying to tell you to instantiate the arraylist like this :

I'm not quite sure whether this is the root of the exeption, but try it anyway. If problem persist then let us know.


Thank you Koushik for clarifying that part. It worked but gave a new error now, "NullPointerExceptionError"
Following is the stack trace for it

when i check line 41 its the list.add(word) in
Any suggestions, what's wrong here.. if you want i have already posted the full code above.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
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.

Winston


Even if the list is static it wont create any problem.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Winston
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is true. Global variables should be avoided if possible. You can avoid using that global variable as well. in that case no need to change the method return type.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I think you are getting NullPointerException because you are declaring list twice. just remove one and make corresponding changes. It should work then.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston for pointing that out. I have fixed it.
And Koushik as always you are spot on but i seem to be in a fix here now, for i cant find anywhere in the code that i have declared List twice
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your code which is working perfectly now. I've just removed one word from your code.

PS: Change the directory as required.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

Do correct me if this interpretation is wrong.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Do correct me if this interpretation is wrong.

Nope. Spot on. But just to make sure you understand completely: Why supply a 0-length array?

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
But just to make sure you understand completely: Why supply a 0-length array?
Winston


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... ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Thanks again Winston, for leveraging confidence in my otherwise skeptical cloud of thoughts.

You're most welcome. And you're doing just fine.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic