computer science

posted by .

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);
}
}

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. computer science

    Here is a question which for some reason I feel very unsure with. Can you guys take a look and see if you agree or disagree with what I have?
  2. JAVA PROGRAM SELECTION SORT USING RECURSION

    JAVA PROGRAM Details: I'm having a trouble with the print lines, i'm not sure where to start but i think I got the rest down correctly. Please if there is a mistake in this program, let me know or correct my current program below, …
  3. Programming Language

    Create a method called countSelected, which takes an array of integers and an integer as input parameters and returns the number of occurences of the integer in the array. Example: data:{2, 1, 1, 8, 1, 0} select:1 ==> 3 public int …
  4. Programming

    The code below shows some of the text for a class called City. Now write a complete public constructor with two formal parameters: a String parameter called cityName, and an int parameter called cityPop. Your code should initialize …
  5. C++ Programming

    Design and create a class named Stats that has an array of 12 doubles as one of its member variables. The values in the array should be set by making 12 calls to a public member function named setValue that accepts two arguments, an …
  6. Programming

    Here is the question that I need help to write the code for the following program in Visual Basic 2010. I am trying to write the code but it has a run time error. If your could helps me to write the code it would be greatly appreciated. …
  7. Simple Array Process

    need help with this generate only the pseudocode. No charting is required, but you will have to incorporate the bubble sort algorithm to ensure the selling prices are in order so you can determine the median selling price. Do not assume …
  8. programming

    m 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 …
  9. computer science

    IHow do you refractor this: public class Employee { private boolean isPartTime; private int seniority; private int monthsDisabled; public Employee(boolean isPartTime, int seniority, int monthsDisabled) { this.isPartTime = isPartTime; …
  10. Java programming

    Hi, I'm trying to figure out how to implement toString() methods into the JavaTest and H2ClassB classes so that the output would be 4 3 7 5 99 3: import java.util.ArrayList; public class JavaTest { ArrayList <JavaTest> list = …

More Similar Questions