In this article, array elements are selected and the elements are printed by using their index values. Arrays are a common way in java to store similar types of items together. Individual values as well as all elements of an array can be easily printed. For printing, for all elements of the array, a "for loop" is usually used, which selects an index from 0 to the length of the array.
The following are several examples of arrays of integer and string types
Int type array example
int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210}; int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}}; int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};
String type array example
String[] strarray = new String[]{"One", "Two", "Three"}; String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}}; String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};
algorithm
Step 1 - Declare the type and define the array.
Step 2 - Specify elements based on array type. These elements can also be entered by the user.
Step 3 - Start at index 0 element. print it out.
Step 4 - Increase the index by 1 and print the next element.
Step 5 - Go to step 4 and continue doing the same until the last element of the array is printed.
Step 6 - For 2D arrays, use two separate loops to control row index and column index respectively.
Step 7 - For N-dimensional arrays, use N loops to control N indexes respectively.
Multiple methods
We provide solution examples using different types.
By using Int type array
By using an array of string type
Let’s look at the program and its output one by one.
Method/Example Type 1: Using an array of integer types
For one-dimensional array
for (int n=0; n<array1.length; n++){ System.out.println(array1[n]); } ;
For two-dimensional array
for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(array2[n][m] + " "); } System.out.println(); }
For three-dimensional array
System.out.println("\nThe 3D Int array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]); };
Example
public class newarr_multidim { public static void main(String[] args) { int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210}; int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}}; int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}}; //printing individual elements by index value System.out.println(array1[1]+ "\n\n" +array2[0][1] + "\n\n" + array3[1][0][1]); //printing all elements System.out.println("\nThe elements in the 1D int array are:\n"); for (int n=0; n<array1.length; n++){ System.out.println(array1[n]); } ; System.out.println("\nThe 2D Int array is:\n "); for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(array2[n][m] + " "); } System.out.println(); } System.out.println("\nThe 3D Int array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]); }; }
Output
22 222 -635533 The elements in the 1D int array are: 11 22 32 42 -52 62 -72 82 -92 210 The 2D Int array is: 11 222 23 42 The 3D Int array is: array3[0][0][0] = 1111 array3[0][0][1] = -22222 array3[0][1][0] = 5533 array3[0][1][1] = 433 array3[1][0][0] = 44533 array3[1][0][1] = -635533 array3[1][1][0] = -777733 array3[1][1][1] = 84433 array3[2][0][0] = 90033 array3[2][0][1] = 84433 array3[2][1][0] = -999933 array3[2][1][1] = 433
Method/Example Type 2: Using String Type Array
For one-dimensional array
for (int n=0; n<strarray.length; n++){ System.out.println(strarray[n]); } ;
For two-dimensional array
for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(strarray2[n][m] + " "); } System.out.println(); }
For three-dimensional array
System.out.println("\nThe 3D String array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]); };
Example
public class newarr_multidim2 { public static void main(String[] args) { String[] strarray = new String[]{"One", "Two", "Three"}; String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}}; String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}}; //printing some elements by index value System.out.println(strarray[1]+ "\n\n" +strarray2[0][1] + "\n\n" + strarray3[1][0][1]); //printing all elements System.out.println("\nThe 1D String array is:\n "); for (int n=0; n < strarray.length; n++){ System.out.println(strarray[n]); } ; System.out.println("\nThe 2D String array is:\n "); for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(strarray2[n][m] + " "); } System.out.println(); } System.out.println("\nThe 3D String array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]); }; }
Output
Two Two2 635533 The 1D String array is: One Two Three The 2D String array is: One1 Two2 Three3 Four The 3D String array is: strarray3[0][0][0] = One33 strarray3[0][0][1] = two33 strarray3[0][1][0] = three33 strarray3[0][1][1] = 433 strarray3[1][0][0] = 44533 strarray3[1][0][1] = 635533 strarray3[1][1][0] = seven33 strarray3[1][1][1] = 84433 strarray3[2][0][0] = seven33 strarray3[2][0][1] = 84433 strarray3[2][1][0] = three33 strarray3[2][1][1] = 433
in conclusion
In the above article, taking Int and String types as examples, Java language is used to print array elements. These examples include 1D array element printing, 2D array element printing, and 3D array element printing. These element-printing methods extend to N-dimensional arrays.
The above is the detailed content of Java program to print array elements. For more information, please follow other related articles on the PHP Chinese website!

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
