Search code examples
javaarraysbubble-sort

The .length function always returns 0


For some reason the .length function in my "Bubblesort()" method always returns zero. Could someone explain why the value of 0 is returned?

import java.util.Random;
import java.util.Scanner;

public class BubbleSortS {
    static int Count;
    static Integer org[];
    static Integer list[] = {  };

    public static void main(String[] args) {

        Scanner eingabewert = new Scanner(System.in);
        System.out.print("Wie viele Zahlen sollen sortiert werden: ");
        Count = eingabewert.nextInt();

        Integer list[] = intList();
        org = list.clone();
        Bubblesort();
        System.out.print("Original: ");
        printArray(org);
        System.out.print("\nSortiert: ");
        printArray(list);
        eingabewert.close();
    }

    public static void printArray(Integer[] toPrint) {
        for(int i = 0; i < toPrint.length; i++) {
            if(i < toPrint.length-1)
            {
                System.out.print(toPrint[i]+ ", ");
            } else {
                System.out.print(toPrint[i]);
            }
        }
    }

    public static Integer[] intList() {
        Integer[] nums = new Integer[Count];
        Random rand = new Random();
        for (int i = 1; i <= nums.length; i++)
        {
            nums[i - 1] = rand.nextInt((int) (System.currentTimeMillis()/1000000000));
        }
        return nums;
    }

    public static void Bubblesort() {
          int n = list.length;
          int temp = 0;
          boolean swapped;
                  do{
                    swapped = false;
                    for (int i=0; i < n-1; ++i)
                    {
                        if (list[i] > list[i+1])
                        {
                            temp = list[i+1];
                            list[i+1] = list[i];
                            list[i] = temp;
                            swapped = true;
                        }
                    }
                    n = n-1;
                  } while (swapped);
    }
}

Output:

Wie viele Zahlen sollen sortiert werden: 5 Original: 443, 322, 183, 574, 108 Sortiert: 443, 322, 183, 574, 108


Solution

  • You should be passing arguments to your methods, instead of declaring static fields. What is happening in this case is called shadowing. You are calling the length method on the un-initialized static variable, instead of your initialized list. You can rewrite your program like this:

    import java.util.Random;
    import java.util.Scanner;
    
    public class Bubblesort {
        public static void main(String[] args) {
            Scanner eingabewert = new Scanner(System.in);
            System.out.print("Wie viele Zahlen sollen sortiert werden: ");
            int count = eingabewert.nextInt();
            Integer list[] = intList(count);
            Integer org[] = list.clone();
            bubblesort(list);
            System.out.print("Original: ");
            printArray(org);
            System.out.print("\nSortiert: ");
            printArray(list);
            eingabewert.close();
        }
    
        public static void printArray(Integer[] toPrint) {
            for(int i = 0; i < toPrint.length; i++) {
                if(i < toPrint.length-1)
                {
                    System.out.print(toPrint[i]+ ", ");
                } else {
                    System.out.print(toPrint[i]);
                }
            }
        }
    
        public static Integer[] intList(int count) {
            Integer[] nums = new Integer[count];        
            Random rand = new Random();
            for (int i = 1; i <= nums.length; i++)
            {
                nums[i - 1] = rand.nextInt((int) (System.currentTimeMillis()/1000000000));
            }
            return nums;
        }
    
        public static void bubblesort(Integer list[]) {
            int n = list.length;
            int temp;
            boolean swapped;
            do{
                swapped = false;
                for (int i=0; i < n-1; ++i)
                {
                    if (list[i] > list[i+1])
                    {
                        temp = list[i+1];
                        list[i+1] = list[i];
                        list[i] = temp;
                        swapped = true;
                    }
                }
                n = n-1;
            } while (swapped);
        }
    }