programming

posted by .

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

  • programming -

    Mind linking to a pastebin file? This is barely readable.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. 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, …
  2. 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 …
  3. 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 …
  4. 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 …
  5. 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. …
  6. computer science

    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 …
  7. 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; …
  8. Computer Programming

    I already posted this question but I gave it a try I'm not sure if I'm on the right track: Make a util class with three methods. First method's name is add, that returns the result by adding two numbers. Second method's name is multiply, …
  9. 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 = …
  10. Java Programming

    Hey! Thanks for checking my question out! ____ 10. If x is a sorted array of int values, which of the following shows a call to a method of the Arrays class that will determine if the value 7 exists in the array?

More Similar Questions