Why cant we call methods in the array subclass using the name of an array in Java?
Do Java's regular arrays have built in methods? - Stack Overflow
Creating an array method java - Stack Overflow
[JAVA] Arrays. ELI5 please.
Videos
why do we have to do:
Arrays.fill(list, 5);
instead of
list.fill(5);
I thought that arrays are objects variables in Java, and usually I can call on a non static instance method using the name of the object.
An array is a reference type, which means it's a sub-class of Object. Therefore all the methods of Object can be invoked on arrays. Perhaps the book meant that arrays don't introduce any array specific methods.
[I is the class name of an int array (int[]).
All methods above you referred is inherited from Object. Object in Java API is an object too. Every class you createed ended with .java is by default inherited from Object. Of course even though an array is an object, and Every thing is an object
Adding to the Sandeep Kakote's answer :
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] squareArry = powArray(new double[]{10,20,30},3);
}
public static double[] powArray (double a[],int z){
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = Math.pow(a[i], z);
System.out.print(b[i]);
}
return b;
}
It seams you are making a recursive method because you call your method in the return return powArray(a);, so this will call your method again and again until your condition is wrong and return -1.
... i need to write a method called powArray that takes a double array, a, and returns a new array
The first part is correct method called powArray that takes a double array, but the second is not return an array it return a double, instead your method signature should look like :
public static double[] powArray (double a[]){
//------------------^^
Now the return part should be like so ;
public static double[] powArray (double a[]){
for (int i = 0; i < a.length; i++) {
a[i] = Math.pow(a[i], 2.0);
}
return a;//<<-------------return the result
}
Test
If you try to make this call in your main method :
public static void main(String[] args) {
double[] a = new double[]{5, 6, 8, 7};
System.out.println(Arrays.toString(powArray(a)));
}
The output :
[25.0, 36.0, 64.0, 49.0]