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]
# 1

> 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.

jverda at 2007-7-9 21:42:28 > top of Java-index,Java Essentials,New To Java...
# 2
do you mind posting your complete program?
lrngjavaa at 2007-7-9 21:42:28 > top of Java-index,Java Essentials,New To Java...
# 3
The first one should be fine, there must some other problem going on.For the second, you do not have curly brace around the if statement so the break is outside and will always be executed.Sheesh. Jos and I could have a race.Message was edited by: flounder
floundera at 2007-7-9 21:42:28 > top of Java-index,Java Essentials,New To Java...
# 4

IMHO this is a better way:

for(int index=0;index<a.length;index++) {

if(a[index] == n) {

return true;

}

}

return false;

>

floundera at 2007-7-9 21:42:28 > top of Java-index,Java Essentials,New To Java...
# 5
> IMHO this is a better way:Agreed.
jverda at 2007-7-9 21:42:28 > top of Java-index,Java Essentials,New To Java...
# 6
And while I'm NOT helping newbs and being rudereturn a[a.length - 1];However this does not take into account if the array is full or not. What happens when the array is 5 elements long and only 3 elements have been entered?
floundera at 2007-7-9 21:42:29 > top of Java-index,Java Essentials,New To Java...
# 7
All fixed and working greatthanks to everyone for there quick responses!
ryankluga at 2007-7-9 21:42:29 > top of Java-index,Java Essentials,New To Java...
# 8
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?
ryankluga at 2007-7-9 21:42:29 > top of Java-index,Java Essentials,New To Java...
# 9

> 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 > top of Java-index,Java Essentials,New To Java...
# 10
However, nobody calls non-static methods in Java "dynamic." Informally, they're referred to as static/non-static. The JLS defines them as "class methods" and "instance methods."
jverda at 2007-7-9 21:42:29 > top of Java-index,Java Essentials,New To Java...