> Hi, trying to find the largest element in an array as follows:
>
[quoted text clipped - 6 lines]
> the max method. I expected it to return the value of the largest
> element, but it seems it's an object?
What kind of Object will the List return?
Has your professor covered the cast operator yet?
That will not be necessary with current Java versions. Has your professor
covered generic types yet?
-- Lew
JamesG כתב:
> Hi, trying to find the largest element in an array as follows:
>
[quoted text clipped - 8 lines]
>
> Any pointers how to achieve this, much appreciated!
Hi,
At the beginning I thought that this answer will be easy, I guess not.
I assume you are using java version 1.5 or 1.6 which supports
generics.
When trying to define the following code:
int[] myArray = {1, 2, 5, 3, 6, 2};
List<Integer> list = java.util.Arrays.asList(myArray);
int max = Collection.max(list);
You will receive an error because asList returns a list with the
following type:
List<int[]> i.e. a list that contains arrays and not as expected List
that contains integers, I assume it happens because java does not do
any autoboxing here.
To make a long story short I use Integer:
Integer[] myArray = {1, 2, 5, 3, 6, 2};
List<Integer> list = java.util.Arrays.asList(myArray);
int val = Collections.max(list);
If you are using java version 1.4 you can use the following code:
Integer[] myArray = {1, 2, 5, 3, 6, 2};
List list = java.util.Arrays.asList(myArray);
Integer val = (Integer) Collections.max(list);
For more information on boxing and generics please read
http://www.google.co.il/search?q=java+generics+tutorial
Lew - 10 Mar 2007 14:59 GMT
> When trying to define the following code:
> int[] myArray = {1, 2, 5, 3, 6, 2};
[quoted text clipped - 5 lines]
> that contains integers, I assume it happens because java does not do
> any autoboxing here.
Nice catch!
List<Integer> raws = Arrays.asList( 1, 2, 5, 3, 6, 2 );
would work, though.
> For more information on boxing and generics please read
> http://www.google.co.il/search?q=java+generics+tutorial
It is a weird effect if you are used to pre-generics syntax, but follows
directly from the rules for autoboxing (specifically, when it doesn't happen)
and the method signature:
List<T> asList( T ... a )
-- Lew
please send me how to create jar file from command prompt
thank you
Lew - 11 Mar 2007 19:27 GMT
> please send me how to create jar file from command prompt
Please open a new thread when you open a new topic.
Try the following from the command prompt (represented here as '$ '):
$ jar --help
$ man jar
and the very first place one would have expected you to have looked for
information on the JAR command and JAR files:
<http://java.sun.com/javase/6/docs/technotes/guides/jar/index.html>
Usually when one has basic questions about Java tools one would start at
<http://java.sun.com/javase/6/docs/technotes/tools/index.html>
and for Java questions generally, one should have looked through
<http://java.sun.com/>
for the information.
-- Lew