如何在Java中打印数组
时间:2020-02-23 14:35:31 来源:igfitidea点击:
在本教程中,我们将看到如何在Java中打印数组。
打印数组有多种方式。
让我们通过几种方法来打印Java中的数组。
使用arrays.tostring()
我们可以使用 Arrays.toString()在Java中打印数组的方法。
这是打印数组的最简单方法。
arrays.tostring()返回字符串对象.This字符串包含字符串格式的数组。
让我们用代码理解
import java.util.*;
public class Main {
public static void main(String args[]) {
int a[]={1,6,7,9,4};
int n = a.length; //a.length returns size of array.
String f = Arrays.toString(a); //Array in string format
System.out.println(f);
}
}
输出:
[1, 6, 7, 9, 4]
使用Deeptostring()方法
基本上 Arrays.toString()适用于1维数组,但它不会适用于多维阵列。
假设我们有二维阵列,如图1 2 3 4 5 6 7 8 9将多维数组转换为串,可以使用 Arrays.deepToString()方法。
这是如何在Java中打印2D数组的方式。
import java.util.*;
public class Main {
public static void main(String args[])
{
int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
String x = Arrays.deepToString(a);
System.out.println(x);
}
}
输出:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
使用Java 8流API
在Java 8中,我们可以将数组转换为流和打印阵列,其中包含流的foreach()方法。
package org.igi.theitroad;
import java.util.Arrays;
public class PrintArrayMain {
public static void main(String args[])
{
System.out.println("================");
System.out.println("Printing 1D String array");
System.out.println("================");
String[] countriesArr = new String[]{"Netherlands", "China", "Japan", "Bhutan"};
Arrays.stream(countriesArr).forEach(System.out::println);
System.out.println("================");
System.out.println("Printing 1D Integer array");
System.out.println("================");
int[] arrayInt = {1, 2, 3, 4, 5};
Arrays.stream(arrayInt).forEach(System.out::println);
System.out.println("================");
System.out.println("Printing 2D String array");
System.out.println("================");
String[][] string2DArray = new String[][]{{"Java", "Python"}, {"Go", "Kotlon"}};
Arrays.stream(string2DArray).flatMap(str -> Arrays.stream(str)).forEach(System.out::println);
System.out.println("================");
System.out.println("Printing 2D String array");
System.out.println("================");
int[][] int2DArray = new int[][]{{10, 20, 30, 40}, {50, 60, 70, 80}};
Arrays.stream(int2DArray).flatMapToInt(i -> Arrays.stream(i)).forEach(System.out::println);
}
}
输出:
================ Printing 1D String array ================ Netherlands China Japan Bhutan ================ Printing 1D Integer array ================ 1 2 3 4 5 ================ Printing 2D String array ================ Java Python Go Kotlon ================ Printing 2D String array ================ 10 20 30 40 50 60 70 80
正如我们所看到的,我们已经看到了如何使用Stream在Java中打印字符串数组和整数数组。
使用循环
我们可以使用Java中的循环和打印数组的循环和打印元素迭代数组。
public class Main {
public static void main(String args[]) {
int a[]={1,6,7,9,4};
int n = a.length; //a.length returns size of array.
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" "); //printing all the elements of array.
}
System.out.println();
//Arrays can be accessed randomly
//Eg : Lets try to print 3rd element of array,
System.out.println("3rd element of array is: "+a[2]);
}
}
输出:
1 6 7 9 4 3rd element of array is: 7
使用每个循环
对于每个与循环相同的工作,但与语法不同。
public class Main {
public static void main(String args[]) {
int a[]={1,6,7,9,4};
for(Integer i : a) //for every iteration, each array element is stored in i.
{
System.out.print(i+" "); //printing all the elements of array.
}
}
}
输出:
1 6 7 9 4
在Java中以相反的顺序打印阵列
我们可以以相反的顺序使用循环迭代阵列并打印元素。
这将以相反的顺序打印阵列。
public class Main {
public static void main(String args[]) {
int a[]={1,6,7,9,4};
int n = a.length; //a.length returns size of array.
for(int i=n-1;i>=0;i--)
{
System.out.print(a[i]+" "); //printing all the elements of array.
}
System.out.println();
}
}
输出:
4 9 7 6 1
使用自定义对象打印数组
如果,我们在数组中有自定义对象,那么我们应该重写(overwriting) toString()自定义类中的方法,否则它将为我们提供意外结果。
让我们在exclue创建一个名为的简单类的帮助下看 Color.java
package org.igi.theitroad;
public class Color {
String name;
String colorCode;
public Color(String name, String colorCode) {
super();
this.name = name;
this.colorCode = colorCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColorCode() {
return colorCode;
}
public void setColorCode(String colorCode) {
this.colorCode = colorCode;
}
}
创建名为的主类 PrintArrayOfColors.java
package org.igi.theitroad;
import java.util.Arrays;
public class PrintArrayOfColors {
public static void main(String[] args) {
Color red = new Color("Red", "#FF0000");
Color blue = new Color("Blue", "0000FF");
Color white = new Color("White", "#FFFFFF");
Color green = new Color("Green", "#008000");
Color[] colorArray=new Color[]{red,blue,white,green};
System.out.println(Arrays.toString(colorArray));
}
}
如果我们注意到,我们没有 toString()方法 Color类,这就是为什么它正在调用对象的ToString()方法,我们无法看到有意义的结果。
我们需要重写(overwriting) toString()方法 Color类,我们将获得信息性的输出。
package org.igi.theitroad;
public class Color {
String name;
String colorCode;
public Color(String name, String colorCode) {
super();
this.name = name;
this.colorCode = colorCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColorCode() {
return colorCode;
}
public void setColorCode(String colorCode) {
this.colorCode = colorCode;
}
@Override
public String toString() {
return "Color{" +
"name='" + name + '\'' +
", colorCode='" + colorCode + '\'' +
'}';
}
}

