-1

Okay so I am trying to replicate my own sorting algorithm into java as Arrays are my weakest link, and as they say, practise makes perfect. I have all the logic I need for it, but I am struggling with the coding as I am still relatively new to Java (any good tutorials will be appreciated).

1) So the first thing I am trying to do is create a user inputted array of any length that has integers. I am basing my code around a string list function I made a while ago except the code does not transfer from String to integer.

The line that the IDE does not like most is when I try and convert:

List<String> numList = new ArrayList<String>();

To

List<int> numList = new ArrayList<int>();

This is probably a syntax error that I am just unable to solve

2) I would like to write something that in my mind would work as:

for(int i = 0; i <= numList.size; i++) {
    int number1 = numList[i];
    int number2 = numList[i + 1];
    int sortedArray[];

    if (number1 >= number2) {
        sortedArray[i] = number2;
        numList[i + 1] = number1;
    } else {
        sortedArray[i] = number1;
        numList[i + 1] = number2;
    }
}

As you can see the logic and what seems like to me the code, is there however it still does not work and with my limited java knowledge I can not work out why.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

2 Answers2

4

1) When it comes down to generics, you need to use classes and not primitive types. As a result Java introduced schizophrenic classes for every primitive type. The primitive type of int is Integer, so you have to use:

List<Integer> numList = new ArrayList<Integer>();

2) Your second question boils down to operator overloading, a concept not (yet) supported by Java. There is however a project described in this answer that helps you to define operators and compiles it to corresponding Java files.

You need to use List<T>#get and List<T>#set to manipulate ArrayList and List objects in general. Furthermore a List<T> has no .length but a List<T>#size method to inspect the size.

Translating your algorithm to a List<Integer> for numList would thus look like:

for(int i = 0; i <= numList.size(); i++) {
    int number1 = numList.get(i);
    int number2 = numList.get(i + 1);
    int sortedArray[];

    if (number1 >= number2) {
        sortedArray[i] = number2;
        numList.set(i + 1,number1);
    } else {
        sortedArray[i] = number1;
        numList.set(i + 1,number2);
    }
}

But it will still fail, as you fail to initialize your sortedArray (regardless of being an array or List<T>). Furthermore it goes out of bounds because of the <= in the for loop (fix with < instead of <=).

3) Your concepts looks a bit like bubble sort: bubble sort

You don't need a sortedArray: you simply need to swap items. Try to think how you can swap items. That being said, it is probably a bit more efficient to use an array, and furthermore there are faster algorithms like MergeSort, QuickSort and HeapSort.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

You can use such kind of implementation. This can also work when you are creating Arrays of objects too:

Int op[] = new Int[int n];
    java.util.Arrays.fill(op, new Int());
    for(int i=0;i < n; i++){
        op[i].get();
        op[i].processFile();
    }
Krishna Chalise
  • 147
  • 1
  • 15