Sort perhaps?

ok so in my program I have an int array (called int_array) that has 200 as it's length. It has already been stored with integer values. The problem is, the integer values in this array should be transfered to another array of the same length and then sorted in numerical ascending order.

What i have so far is this:

new_array = int_array;

new_array has already been initialized as having 200 as it's length and an integer value. Practically, I just don't know how to sort that array into ascending order. From the java site i found what's supposed to be put inside sort:

sort(int[] a)

but i'm not sure really how to use this. I was thinking:

new_array = sort(int_array a);

but it doesn't seem to be working

[762 byte] By [RAWR-itsanONIONa] at [2007-11-27 7:43:10]
# 1

The Collection Framework comes with sorting algorithms:

http://java.sun.com/docs/books/tutorial/collections/algorithms/index.html

By the way, assigning one array variable to another:

array1 = array2;

is not what you want to do. It causes array1 to point the same array object as array2 -- it does not copy array contents. Use System.arraycopy for that.

Hippolytea at 2007-7-12 19:23:58 > top of Java-index,Java Essentials,Java Programming...
# 2

> ok so in my program I have an int array (called

> int_array) that has 200 as it's length. It has

> already been stored with integer values. The problem

> is, the integer values in this array should be

> transfered to another array of the same length and

> then sorted in numerical ascending order.

>

> What i have so far is this:

>

> new_array = int_array;

This does not copy any values. It just causes the new_array reference variable to point to the same array object that the int_array reference variable points to.

You need to create a new int[] with the new operator and then use System.arraycopy to copy the values over.

>

> new_array has already been initialized as having 200

> as it's length and an integer value.

If you did new_array = new int[200] right before the above assignment statement, then the array you just created is lost as soon as you do new_array = int_array because nothing points to it anymore.

> Practically, I

> just don't know how to sort that array into ascending

> order.

After copying the values, use java.util.Arrays.sort.

> but i'm not sure really how to use this. I was

> thinking:

>

> new_array = sort(int_array a);

You need to go back to the very beginning and learn about classes and objects and how to call methods. You're missing some very important fundamentals and you won't get anywhere until you learn them.

Also, naming conention in Java would be intArray and newArray--caps instead of underscores to separate words.

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].

James Gosling's [url=http://www.bookpool.com/sm/0321349806]The Java Programming Language[/url]. Gosling is

the creator of Java. It doesn't get much more authoratative than this.

jverda at 2007-7-12 19:23:58 > top of Java-index,Java Essentials,Java Programming...