Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Using Collections to find max element in array

Thread view: 
JamesG - 10 Mar 2007 13:52 GMT
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!
Lew - 10 Mar 2007 14:32 GMT
> 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
Efi Merdler - 10 Mar 2007 14:44 GMT
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
asithatruth@gmail.com - 11 Mar 2007 17:25 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.