Some help with basic array operations
Im stuck on a fairly easy array method, Im trying to get the last value of an array what I had so far was:
public static int lastLocation(int a[])
{
int last = a.length-1;
return a[last];
}
and it keeps coming up ArrayIndex o.b.e, and i cant seem to get it to properly work
and one more method, In the same project I am trying to return a boolean if a number is in an array or not and so far I have:
public static boolean present(int a[], int n)
{
boolean found=false;
for(int index=0;index<a.length;index++)
{
if(a[index] == n)
found = true;
break;
}
return found;
}
and every time I call that method, It only finds the first value of the array, but the loop looks fine to me, I just dont understand.
Thanks for everyone's time.>
[870 byte] By [
ryankluga] at [2007-11-26 19:22:11]

> public static int lastLocation(int a[])
> {
> int last = a.length-1;
> eturn a[last];
> }
This is the correct way to do it. You must be getting that error from somewhere else or from an old version of that code.
> and one more method,
if (...)
found = true;
break;
is the same as
if (...) {
found = true;
}
break;
You should get in the habit of always using braces with if/for/etc.
> one more question, for this work my professor wants
> the static methods and drivers and also 2 more files
> of dynamic methods and dynamic drivers....whats the
> difference?
static method is a class method, so you can run them without creating a new instance of the class, the dynamic method I guess is an instance of class' method.
static method
class A {
static void staticMethod() {
System.out.println("A Class");
}
}
class B {
void dynamicMethod() {
System.out.println("Instance of B");
}
}
.......
A.staticMethod();
B b = new B();
b.dynamicMethod();
ot6a at 2007-7-9 21:42:29 >
