you could simply put the names in a list like ArrayList and use the helper class java.util.Collections to shuffle this list with Collections.shuffle(). Then you don't need a random number generator and you don't have to worry that each name should only be displayed once.
Joined: Apr 29, 2008
Thank you so much,
but I don't want to use ArrayList. Is any other solution for my problem? Because I should write this program without ArrayList.
Sounds interesting but unfortunately I don't know what you mean Could you please explain? Are we both talking about the method asList() of class java.util.Arrays? According to the JavaDoc this method returns a regular (fixed-size) java.util.List with the content of the array parameter.
Marco [ May 11, 2008: Message edited by: Marco Ehrentreich ]
Joined: Oct 13, 2005
Yes, we are talking about the asList() method, which does return a fixed-size List object. Unfortunately that List object declares itself of a class called ArrayList. This has confused me, and several other Ranchers, when we have tried methods of java.util.ArrayList on it.
And java.util.ArrayList is the ArrayList class we all know and love so well.
Actually I've never worried about this fact because the API documentation just indicates that asList() returns a List<T>. At least the API of JDK 1.6 does so. Where did you get that information? From the source code of the JDK?
Sorry for asking stupid questions but I can't see where the problem is The API doc (J2SE 1.6) says this method
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
It doesn't even mention that it has to do something with any kind of ArrayList.
If a method is defined to return an interface type, then you should only rely on any object that is returned from it implementing the methods of that interface. If you find out what actual object type is returned (by using a debugger or looking at the source code) and then make use of that knowledge in your code, then you are asking for trouble because different versions of the JRE may return a completely different object type. The only thing you can rely on is that that object will implement the stated interface.
This is exactly what I thought, too. I don't see any problems if you stay with the interface return type.
And if you really need a specific implementation like ArrayList it would be no problem to create one with the List you have.
Joined: Sep 17, 2006
[JN]Then what if one needs very specific and predictable behaviour of an interface according to the specific job requirements? Should that become a return type of a class that one designs in adherence to design specifications, relying on the generality of library interfaces where such generality is provably correct? We may need the unpredictable behvaiour as stated in such things as HashSet. Isolating the design from critical review - such as dismissing Hashtable - when the behaviour was intentionally designed in at first coding is an issue for Team Lead or Project Manager. The decision is a consequence of installation / site needs and general design environment. If the return type from Arrays.asList() is a List, then that signature fulfills the generality issue. If the behaviour of the list may vary according to what was used to generate the list, it is my opinion that the coder needs a way to compartmentalize the behaviours and determine the behaviour of the code when it is coded, not in a failure incident.
I have been presented with that situation ( enforcing generality by fiat ) enough times that I will code around it internally, and would if called present an interface return type from the library - if and when that is asked for. Not before. [ May 13, 2008: Message edited by: Nicholas Jordan ]
Then what if one needs very specific and predictable behaviour of an interface according to the specific job requirements?
That is immaterial. Why should it matter? If your return type is an interface, all you can depend on is the defined functionality of that interface, not how it was implemented. Coding a solution that relies on the implementation of that interface is coding bugs into your software.
Of course you can code around the situation (I assume you are talking about using instanceof to check the actual type of the returned object), but this could lead to situations where your code will only run on certain versions of the JRE. Versions of the JRE that don't return the type of object you are expecting will not be able to run your code fully.
If you've written a method that has a Set return type, but you always call it from code that requires a HashSet, then that is bad design. If you have to have a HashSet returned from a method then define it to return a HashSet. If you define it to return a Set, then someone else may modify that method to return a different type of Set and so inadvertently break your code that calls the method. [ May 13, 2008: Message edited by: Joanne Neal ]
Joined: Sep 17, 2006
Joanne + Paul - I work in a smaller shop where who uses the code and how may be tightly defined at project definition. Both of you have had to deal with the situation ( obviously ) in settings where who uses the code and how - must apriori - be versitile and reliable in the sense that conformance to established libs is something like paint on the wall or water coolers cool water.
Joanne's have to have is what I was looking for, as ( possibly ) is the original poster. Of course, we can advise OP to call collections.randomize() - and if that is the reliable paradigm, then we should advise so. This avenue would show OP why.
I still think, there's no need to "code around" something or to count on a specific implementation of an interface.
The API definitely says Arrays.asList() returns a List (interface) which is guaranteed to be a List and not more. And if you really need additional features of an ArrayList for example you can easily built one by using ArrayList's overloaded constructor which takes a collection. No need to deal with problems of a specific version or JDK dependent implementation. No need be stuck with a simple List interface.
So where is the problem here? Or am I missing something?
Marco [ May 13, 2008: Message edited by: Marco Ehrentreich ]
Ordinarily the implementing class shouldn't matter. For some reason though, Nagmeh doesn't want to use ArrayList, so I guess that's why Campbell mentioned the fact that Arrays.asList() returns an instance of a class called ArrayList, even though it's not a java.util.ArrayList. We don't know why Nagmeh doesn't want to use an ArrayList, so we don't know if this matters. Perhaps it's for a school assignment, and the instructor doesn't want them to use too many standard libraries to make their lives easier. (That's my guess.) Let's get Nagmeh to explain the requirements a little more, rather than saturating him/her in explanations of things that may well be outside of the intended problem space.
Nagmeh, if you print the names "randomly", are you supposed to make sure that each name occurs exactly once? Or can the names repeat? And are you allowed to use other classes and methods like Arrays.asList() or Collections.sort()?