Im trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code:
public class Testing {
private int [] data;
private int count;
private static int defCapacity = 10;
public Testing (){
count = 0;
data = new int [defCapacity];
}
public Testing (int cap){
count = 0;
data = new int [cap];
}
public void add(int val){
if (count < data.length){
data[count] = val;
++count;
}
else {
increaseCapacity();
data[count] = val;
++count;
}
}
public void add (int index, int val){
if (index <0 || index > count)
throw new IndexOutOfBoundsException ("Invalid index");
if (count == data.length)
increaseCapacity();
for (int i = count-1; i >= index; i--)
data[i+1]= data[i];
data[index] = val;
++count;
}
public int remove (int index){
if (index <0 || index >=count)
throw new IndexOutOfBoundsException ("Invalid index");
int r = data[index];
for (int i = index; i <count-1;i++)
data[i] = data[i+1];
--count;
return r;
}
public void clear (){
count = 0;
}
public boolean contains (int val){
return (indexOf (val)!=-1);
}
public int indexOf (int val){
for (int i=0; i <count; i++)
if (data[i]==val)
return i;
return -1;
}
public String toString (){
String r = "[";
for (int i =0; i <count-1; i++)
r += data[i]+", ";
r += data[count-1]+"]";
return r;
}
public int size (){
return count;
}
private void increaseCapacity() {
int [] newData = new int [data.length*2];
for (int i =0; i <data.length; i++)
newData[i] = data[i];
data = newData;
}
public static void main (String [] args){
Testing ourList = new Testing();
ourList.add(2);
ourList.add(134);
ourList.add(15);
ourList.add(6);
System.out.print(ourList);
}
}
To sort the numbers in your ArrayList without using the `Arrays.sort()` method, you can implement a sorting algorithm such as the bubble sort or insertion sort. Let's go with the insertion sort algorithm in this example:
First, add a new method called `sort` to your `Testing` class:
```java
public void sort() {
for (int i = 1; i < count; i++) {
int current = data[i];
int j = i - 1;
while (j >= 0 && data[j] > current) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = current;
}
}
```
In this method, we iterate through the array starting from the second element. For each element, we store it in a variable called `current`. We then compare `current` with the previous element(s) and move any larger elements one position to the right. This creates a space for us to insert `current` at the correct position within the sorted subarray.
Next, call the `sort` method after adding numbers to your `ourList` object in the `main` method:
```java
public static void main (String [] args){
Testing ourList = new Testing();
ourList.add(2);
ourList.add(134);
ourList.add(15);
ourList.add(6);
ourList.sort();
System.out.print(ourList);
}
```
Now, when you run your program, the numbers will be sorted in ascending order before printing using the `toString` method.
Note that this implementation modifies the original array rather than creating a new sorted array. If you need to keep the original array intact, you can create a copy of the array before sorting or use a different data structure like a PriorityQueue.