I think that I understand the concept fairly well, but I appear to be having some issues with syntax perhaps.

I have 2 classes. My main class and BubbleSort.java. In my main class I receive values from the user and store them in "int array[]". The values appear to have stored correctly because I can use a for loop to read them back out.

Next I want to call something along the lines of:

BubbleSort(array[], array.length);

to send the array to the bubblesort procedure. This is how I have my bubble sort started.

public class BubbleSort{
       public int trade(int[]array, int length){

I don't think however that the error is in the sorting method itself. My best guess is that I goofed with passing the array. Can someone offer some guidance?

Regards,
Dragonfyre

I'm rusty with Java but...
Watch your [] positioning. Also array is an int [] not a class!
But you have array.length

BubbleSort( array[], length ); 

public class BubbleSort
{
//   public int trade( int[]array, int length)
      public int trade( int array[], int length )
     {

Can we see your bubble sort?

Sure... here is what I have:

private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        String ans = JOptionPane.showInputDialog(null, "Length of Array to Bubble Sort: ", "Bubble Sort", JOptionPane.QUESTION_MESSAGE);

        int n = Integer.parseInt(ans);
        int array[] = new int[n];

        for(int count = 0; count < array.length; count++){
            String Value = JOptionPane.showInputDialog(null, "Enter a Value: ", "Bubble Sort", JOptionPane.QUESTION_MESSAGE);
            int temp = Integer.parseInt(Value);
            array[count] = temp;
        }

        BubbleSort(array[], length);

            jTextArea1.append("Sorted Values:\n");
        for(int count = 0; count < n; count++){
            jTextArea1.append(" "+array[count]+" ");
        }
    }

And the sort (bubble sort is my favorite, please don't judge. :D

/**
 *
 * @author Dragonfyre
 */
public class BubbleSort{

  public int trade(int array[], int length){
    int temp=0;
    for (int count = 1; count < length; count++) {
      for (int index = 0; index < length - 1; index++) {
        if (array[index] > array[index] + 1) {
          temp = array[index];
          array[index] = array[index+1];
          array[index+1] = temp;
          return array[index];
        }
      }
    }
    return 0;
  }
}

The compiler doesn't like line 14 of the first segment of code. It has underlined the phrase "BubbleSort." I have tried multiple things, but java is just not my thing... thus why I am trying to teach myself :D I would appreciate any help.

Regards,
Dragonfyre

Okay. So I changed the line 14 and added a line.

BubbleSort trade = new BubbleSort();
trade.trade(array, n);

I am no longer getting an error and can compile the program. But when I execute it, I get the values in the order I entered. Perhaps if someone could explain how the passing and return of an array works, that may help me more. I thought that I could pass the array to trade() and it would sort the array and return it. Then my main class would continue from that line with the newly sorted array. So if I use it, like I do 2 lines later, it will be sorted. Am I mistaken?

JAVA array is reference type.

I think following example help you to understand array argument and return type.

public class Arr{
   public static int []getArr(int []ar) {
         ar[0]++;
         ar[1]++;
         return ar;
   }
   public static void main(String []args) {
         int m[]={10,20};
         int n[];
         n=getArr(m);
         System.out.println(n[0] + " " + n[1]); // 11 21
         System.out.println(m[0] + " " + m[1]); // 11 21

         n[0]=22;
         m[1]=33;
         System.out.println(n[0] + " " + n[1]); // 22 33
         System.out.println(m[0] + " " + m[1]); // 22 33
   }
}

your sort needs to return the whole sorted array, not just one element. (Remember the problem with the constructor earlier?)

PLease ignore my previous post - doesn't address the real problem. Sorry.

... and, as adatapost points out, arrays are passed by reference, so you don't need to return anything

... and if you make the method static:

public static void sort(int[]...) ...

you can call it without creating an instance, as in

BubbleSort.sort(array...);

I suspect your problem lies in the call on line 14. You pass a variable "length", but I can't see any declaration or initialisation for this - presumably its declared in the class, and maybe its value is 0?

Thanks to every1 for their responses. I did plenty of reading and believe that it now works. Thanks for your guidance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.