Hey, can someone help me explain my own code?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bubbleapplication;

/**
 *
 * @author Inacio Nunes
 */


//Java Collection Framework
import java.util.Arrays;
public class BubbleApplication {

    /**
     * @param args the command line arguments
     */

public static void main(String[] args) {
    
        //int variable was created to store these default values
        int[] unsorted={1,5,8,2,7,4};
        //addElement is used to link with 
        unsorted = addElement(unsorted , 1909);
        int[] sorted=bubblesort(unsorted);
        
        for (int i = 0; i<sorted.length; i++) {
            System.out.print(sorted[i] + ", ");
        }
    
    }

static int[] addElement(int[] a, int e) {
    a  = Arrays.copyOf(a, a.length + 1);
    a[a.length - 1] = e;
    return a;
}

static int[] bubblesort(int[] _2sort ) {
        int temp;
        for(int i=1; i<_2sort.length; i++) {
            for(int j=0; j<_2sort.length-i; j++) {
                if(_2sort[j]>_2sort[j+1]) {
                    temp=_2sort[j];
                    _2sort[j]=_2sort[j+1];
                    _2sort[j+1]=temp;
                }
                
            }
        }
        return _2sort;
    }

    
}

Hey there, welcome to the forum.

The fact that you cannot explain what’s happening does beg the question of whether it is your code, as you claim…

If you wrote it, you ought to know what it is doing, no?

3 Likes

Ha ha, Yes you are absolutely correct, Let me put it this way…
I told my friend to help me… so he did like 99% of the work and I was just processing it in my brain.
I’m new to Java.

What don’t you understand? If it is the bubblesort method then I’d suggest reading up on the bubble sort algorithm.

If it is the addElement method, then all it is doing is copying the original array and extending the size by 1 and adding the desired int into the new last spot.