Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet39/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   35   36   37   38   39   40   41   42   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

The foreach Loops
JDK 1.5 presented another for construct, which is known as foreach loop or extended for
loop.  This  construct  empowers  you  to  cross  the  complete  array  successively  without
utilizing an extra variable.
Sample Implementation:
public class Mytestarray {
public static void main(string[] args) {
double[] myarray = {0.5, 1.2, 2.2, 3.4, 4.7};
for (double i: myarray) {
System.out.println(i);
}
}
This would deliver the accompanying result:
0.5 1.2 2.2 3.4 4.7
Passing Arrays to Methods:
Generally, just as you can pass primitive values to methods or functions, you can likewise
pass arrays to systems. Case in point, the accompanying method shows the components in
an int array:
public static void printarr(int[] arr) {
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k] + ” “);


}
You  can  summon  it  by  passing  an  array.  Case  in  point,  the  accompanying  declaration
conjures the printarr function to show the elements of the array.
printarr(new int[]{0, 3, 5, 3, 1});
The compilation and execution of this code yields the following result:
0 3 5 3 1

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling