Friday
November 21, 2014

Homework Help: computer science

Posted by Adam on Sunday, February 24, 2013 at 1:19am.

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

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

Simple Array Process - need help with this generate only the pseudocode. No ...
programming - m trying to create an arraylist that's sorted but I have no idea ...
Programming - Here is the question that I need help to write the code for the ...
JAVA PROGRAM SELECTION SORT USING RECURSION - JAVA PROGRAM Details: I'm having a...
CIS/Visual Logic - Design an application that accepts 10 numbers and displays ...
Math Statistics - The sorting algorithm known as bogosort can be described in ...
computer science 104 - Create a method areaOfaTrapeziod when given a, b, and h (...
Programming - 2. Then it should prompt the user for the input file name of the ...
Programming - Create the logic for a program whose main() method prompts a user ...
computer science - Create a method called countZeros, which takes an integer ...

Search
Members