Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to get this method to add an array and sort the array without using Arrays.sort

  1. #1
    Junior Member Grafight's Avatar
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trying to get this method to add an array and sort the array without using Arrays.sort

    I'm making a program that will add values to an array and then sort the values, however, i have to do it under the same method and i cannot use Arrays.sort. I feel like my code makes perfect sense but it isn't changing any part of the array for some reason.

    Here's my class file:

    public class IntList
    {
     
       int[] list;
       private int[] tempArray;
       private int numElements = 0;
       private int temp=0;
       //-------------------------------------------------------------
       // Constructor -- creates an integer list of a given size.
       //-------------------------------------------------------------
       public IntList(int size)
       {
          list = new int[size];
       }
       //------------------------------------------------------------
       // Adds an integer to the list. If the list is full,
       // prints a message and does nothing.
       //------------------------------------------------------------
       public void add(int value)
       {
     
     
          if (numElements == list.length)
          System.out.println("Can't add, list is full");
          else
          {
             list[numElements] = value;
             numElements++;
          }
     
     
          for (int i = 0 ; i < numElements; i++)
          {
     
     
             if (list[i] > list[i+1])
             {
                 temp = list[i];
                 list[i] = list[i+1];
                 list[i] = temp;
             }
          }
     
       }
     
     
     
     
     
     
     
       //-------------------------------------------------------------
       // Returns a string containing the elements of the list with their
       // indices.
       //-------------------------------------------------------------
       public String toString()
       {
          String returnString = "";
          for (int i=0; i<numElements; i++)
          {
             returnString += i + ": " + list[i] + "\n";
          }
          return returnString;
       }
    }

    Here's the main:

    public class ListTest
    {
       public static void main(String[] args)
       {
          IntList myList = new IntList(10);
          myList.add(84);
          myList.add(27);
          myList.add(250);
          myList.add(18);
          myList.add(94);
          myList.add(8);
          myList.add(87);
     
          System.out.println(myList);
       }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trying to get this method to add an array and sort the array without using Arrays.sort

    First of all: Your sort loop doesn't swap anything, so nothing is changed, and the items remain in the order in which they were inserted into the list. (The good news here, is that your results seem to validate the part that inserts new items into the array and keeps count. The bad news is that it doesn't sort the array.)

    Secondly: It's very simple: If you are trying to do a bubble sort on the array, then generally speaking, when an array has more than two elements, you can not (that's not) do a bubble sort with one pass through the array. Period.

    Don't you have examples in your textbook or class notes or whatever learning material you are using? Don't they use nested loops?


    Cheers!

    Z

  3. #3
    Junior Member Grafight's Avatar
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get this method to add an array and sort the array without using Arrays.sort

    Idk what a bubble sort is but i found that all i need is the insertion sort algorithm, which i happened to find online so all is well now, thanks.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. array bubble sort
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2012, 05:53 PM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 03:27 PM
  5. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: December 1st, 2008, 09:02 AM