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;
}
Answer from Sourav Sachdeva on Stack Overflow
I am new to java programming
and i am stuck, I tired to make a program that will find the mean of all numbers lsited in a array
here is my code
please send help
public class Main {
public static void main(String[] args) {
double random_numbers[] = {1.3, 5.6, 8.4, 4.1, 5.5, 11.8, 8.5};
System.out.println(random_numbers[(1+2+3+4+5+6+0)/random_numbers.length]);
}
}
Creating an array method java - Stack Overflow
How do I declare and initialize an array in Java? - Stack Overflow
How to make an array of arrays in Java - Stack Overflow
Does Java ArrayList implement an array or list abstract data type?
Videos
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]
You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).
For primitive types:
int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};
// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort
For classes, for example String, it's the same:
String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};
The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.
String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
There are two types of array.
One Dimensional Array
Syntax for default values:
int[] num = new int[5];
Or (less preferred)
int num[] = new int[5];
Syntax with values given (variable/field initialization):
int[] num = {1,2,3,4,5};
Or (less preferred)
int num[] = {1, 2, 3, 4, 5};
Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.
Multidimensional array
Declaration
int[][] num = new int[5][2];
Or
int num[][] = new int[5][2];
Or
int[] num[] = new int[5][2];
Initialization
num[0][0]=1;
num[0][1]=2;
num[1][0]=1;
num[1][1]=2;
num[2][0]=1;
num[2][1]=2;
num[3][0]=1;
num[3][1]=2;
num[4][0]=1;
num[4][1]=2;
Or
int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };
Ragged Array (or Non-rectangular Array)
int[][] num = new int[5][];
num[0] = new int[1];
num[1] = new int[5];
num[2] = new int[2];
num[3] = new int[3];
So here we are defining columns explicitly.
Another Way:
int[][] num={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} };
For Accessing:
for (int i=0; i<(num.length); i++ ) {
for (int j=0;j<num[i].length;j++)
System.out.println(num[i][j]);
}
Alternatively:
for (int[] a : num) {
for (int i : a) {
System.out.println(i);
}
}
Ragged arrays are multidimensional arrays.
For explanation see multidimensional array detail at the official java tutorials
Like this:
String[][] arrays = { array1, array2, array3, array4, array5 };
or
String[][] arrays = new String[][] { array1, array2, array3, array4, array5 };
(The latter syntax can be used in assignments other than at the point of the variable declaration, whereas the shorter syntax only works with declarations.)
try
String[][] arrays = new String[5][];