Reputation: 321
hello I am having difficulty writing this code, I am lost on the last two methods. This is a learning exercise (not homework), but I need examples to learn. Plus I thought this would be useful in the stackoverflow database as well.
public class NumberList {
public int[] values;
public NumberList() {
values = new int[0];
}
public NumberList(int[] a) {
values = new int [a.length];
for (int i=0;i<a.length;i++)
values[i] = a[i];
}
public int getSize() {
return this.values.length;
}
public int getAt(int index) {
if (index>=values.length){
throw new IndexOutOfBoundsException ("Values of out of bounds");
}else{
return values[index];
}
}
public long getTotal() {
long sum = 0;
for (int i=0; i<values.length; i++) {
sum = sum + values[i];
}
return sum;
}
// need help here its a boolean that the number is in the array but if not its //false
public boolean contains(int number) {
for (int i=0; i<values.length; i++){
if (number <values.length+1){
return true;
}
//else
// return false;
// }
// this is an add method that confuses me and ask myself why since i added without it.
public void add(int number) {
number=0;
}
}
Upvotes: 0
Views: 248
Reputation: 533880
Some of these methods can be simplified.
public NumberList(int[] a) {
values = a.clone();
}
public int getAt(int index) {
return values[index]; // throws ArrayIndexOutOfBoundException if out of bounds. It include the invalid index as well.
}
public long getTotal() {
long sum = 0;
for (int i: values) sum += i;
return sum;
}
public boolean contains(int number) {
for (int i: values)
if (number == i)
return true;
return false;
}
public void add(int num) {
int[] values2 = new int[values.length+1];
// arraycopy is typically faster than using a loop.
System.arraycopy(values,0,value2,0,values.length);
values2[values.length] = num;
values = values2;
}
Upvotes: 1
Reputation: 421290
Great start. All you needed to change was the following:
public boolean contains(int number) {
for (int i=0; i<values.length; i++) {
if (values[i] == number) {
return true;
}
// Since this line is reached only if no values matched, you simply do...
return false;
}
Since you're still learning, I'll give you a few more pointers as a bonus :-)
I would change
if (index >= values.length)
to
if (index < 0 || index >= values.length)
You could make use of for-each loops here and there. You could for instance write:
public long getTotal() {
long sum = 0;
for (int i : values)
sum = sum + i;
return sum;
}
Upvotes: 1
Reputation: 13433
First method:
public boolean contains(int number)
{
for (int i=0; i<values.length; i++)
{
if (number == values[i])
{
return true;
}
}
return false;
}
Second Method: For this you have to maintain a variable called current
that contains the index of the most recently added element.
public void add(int number)
{
if(current == values.length - 1)
{
throw new RuntimeException("Array Overflow");
}
values[++current] = number;
}
Upvotes: 0
Reputation: 634
public boolean contains(int number) {
for (int i=0; i<values.length; i++)
if (number==values[i]) return true;
return false;
}
public void add(int number) {
int[] tmp = new int[value.length+1];
for (int i=0; i<values.length; i++) tmp[i] = values[i];
tmp[tmp.length-1] = number;
values = tmp;
}
Upvotes: 5
Reputation: 20820
Should be something like below:
public boolean contains(int number) {
for (int i=0; i<values.length; i++){
//so the number was found
if (number==values[i]){
return true;
}
return false;
}
You can also use Arrays.binarySearch() method instead of writing one yourself. The binary search will find if the number is in the array and what index (of course the array has to be ordered).
Upvotes: 1