Using Collections to find max element in array

J

JamesG

Hi, trying to find the largest element in an array as follows:

int[] myArray = {1, 2, 5, 3, 6, 2};

Collection col = new ArrayList(java.util.Arrays.asList(myArray));
Object MAX_E = Collections.max(col);

I have gotten this far, however i'm unsure how to access the result of
the max method. I expected it to return the value of the largest
element, but it seems it's an object?

Any pointers how to achieve this, much appreciated!
 
L

Lew

JamesG said:
Hi, trying to find the largest element in an array as follows:

int[] myArray = {1, 2, 5, 3, 6, 2};

Collection col = new ArrayList(java.util.Arrays.asList(myArray));
Object MAX_E = Collections.max(col);

I have gotten this far, however i'm unsure how to access the result of
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
 
E

Efi Merdler

JamesG כתב:
Hi, trying to find the largest element in an array as follows:

int[] myArray = {1, 2, 5, 3, 6, 2};

Collection col = new ArrayList(java.util.Arrays.asList(myArray));
Object MAX_E = Collections.max(col);

I have gotten this far, however i'm unsure how to access the result of
the max method. I expected it to return the value of the largest
element, but it seems it's an object?

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
 
L

Lew

Efi said:
When trying to define the following code:
int[] myArray = {1, 2, 5, 3, 6, 2};
List<Integer> list = java.util.Arrays.asList(myArray);

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.

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
 
L

Lew

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top