• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert list to array  RSS feed

 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, can anyone help how can I convert list to array?

this gives me an output
1
7
4
null
null
null
null
null
null
null
null
null
[null, 7, 4, null, null, null]
So, I don't understand why converts list to array with all values of null. and in the Internet the most common answer is this. I think I can do it with iterating through my List but this would be ugly way of doing it because for example I would have 1000 elements in my list and I changed only 1 I would have to make all 999 unneeded assignments and it would take a long time to execute my program may be anyone know better way of doing this???
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything look strange about this loop?

Hint - look at the index variable(s).

Edit: actually, I'm not sure why that compiles. Maybe i is declared as a class variable for some reason? Or is that not the correct code?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote:
So, I don't understand why converts list to array with all values of null.


It doesn't. It converts the list to an array with the same elements. As we can see from the output, it's got the null at the front that you set, and then the 7 and 4 that were there, and then the rest of the nulls.

I would have 1000 elements in my list and I changed only 1 I would have to make all 999 unneeded assignments and it would take a long time to execute my program may be anyone know better way of doing this???


Sorry, but it's not really clear to me what problem you're having. And your code is confusing the issue. Next time, please provide an SSCCE that contains everything necessary to demonstrate the issue and nothing else.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote:Hello, can anyone help how can I convert list to array?

You need to TellTheDetails. What you have so far is an array (presumably of size 12) and a List, both defined outside the code you've shown.

But in answer to your basic question: the way to convert an array to a List is with Arrays.asList(), and the way to convert a List to an array is with one of its toArray() methods.

Beyond that, it's difficult to help without more information.

Winston
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try adding to list.toArray(new Element/Object[list.size()]);

Here watch this:


I don't remember the full explanation of why, but I remember that you had to put the type even if it's with generics.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a collection that does not contain any nulls by using a Queue implementation that does not allow nulls. Search here: http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html or alternatively you could create your own implementation of List and overwrite the Add() and AddAll() methods and exclude nulls before adding data to the list.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor M. Pereira wrote: . . .

Here watch this:
. . . .
Watch indeed! That method does not do what you think it does.

Anissa Pary, you have already been told you do not need to test for nullity to prevent the Exception you are suffering.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method may even fail to compile.
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That method may even fail to compile.


It works on eclipse!

I tested it. On the code I wrote.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happened when you passed an Integer[] containing nulls?
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what happened when you passed an Integer[] containing nulls?


Well, then the test would fail!

Which would mean the conversion failed, wouldn't it?

Since even if there was a null and it thrown NullPointerException or CastClassException the intention of the test would be fulfilled. Since a failed assertion is an exception as well.

The method assertNotNull real code is:



The reason there is not even one null is that you specified size in the toArray(T[]), the assert method is just checking that your assumptions are correct.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor M. Pereira wrote: . . . The method assertNotNull real code is:

. . . .
No, it isn’t. That has two parameters and you are only passing one argument. You must be calling another method.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor M. Pereira wrote:
Campbell Ritchie wrote:And what happened when you passed an Integer[] containing nulls?


Well, then the test would fail!
You didn’t answer my question. You obviously didn’t try it with nulls in your array.
Since even if there was a null and it thrown NullPointerException or CastClassException the intention of the test would be fulfilled. . . . .
Not at all. It means the test is never attempted. You would, however, realise something has gone wrong.
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No what I mean is the code I showed you is junit Assertion code implementation that I found in the net. (i.e. not mine but from the library that people download to use junit)

You did see the assertNotNullsOnArray passing the reference for the thing I just converted right?

If there were any nulls on the conversion the test would fail. Since any uncatched exception is a red bar.

Sometimes I wonder how did we end in a testing framework argument.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case the testing software is not giving fully correct information about the test. I am susprised that code compiles at all, since you are passing primitives to a method which takes reference types as its parameters.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!