導航:首頁 > 編程語言 > java列印string數組

java列印string數組

發布時間:2024-03-16 10:19:27

java 字元數組如何列印

可以同過普來通的源for循環,有數組名.length得到數組長度來循環列印,也可以通過增強的for循環的方式來列印。例如:

publicclassPrintArray
{
publicstaticvoidmain(String[]args)
{
char[]ch={'a','b','c','d','e','f','g'};
for(inti=0;i<ch.length;i++)
System.out.print(ch[i]+"");
System.out.println();
for(charc:ch)
System.out.print(c+"");
System.out.println();

}
}

⑵ java如何讓數組里的字元串循環輸出

通過for循環後把數組中的字元串輸出

1、定義字元串數組

Stringarr[]=newString[]{"a","b","c"};//定義一個字元串數組arr

2、循環數組

for(inti=0;i<arr.length;i++){//通過arr.length獲取字元串數組長度
System.out.println(arr[i]);//循環輸出字元串數組元素
}

⑶ java 怎麼把字元串數組完全輸出出來

直接通過判斷list數組的長度,之後循環輸出每一個元素即可。
public class Test {
public static void main(String[] args) {
String [] strs = {"asd", "fdfds", "233"};
//用版for each 語句輸出權
for(String s: strs) {
System.out.println(s);
}
//常規循環輸出
for(int i = 0; i < strs.length; i ++) {
System.out.println(strs[i]);
}
}

}

⑷ java 中 如何 輸出 (列印出)字元數組 CharSequence[] chars; 直接 chars.toString() 列印的是地址

⑸ Java重寫toString 使之可以輸出二維數組

java的標准API中的Array類沒有toString方法,因此沿用父類Object的toString方法:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
而Array類中的方法幾乎全是本地方法,可以說Array類就是一個本地類,因此他不受java語法約束,也根本無法用java語法改寫toString方法,想要改寫這個類實在是太難了,我是不會..
但是你可以使用java.util.Arrays類中的deepToString(Object o[])靜態方法,可以實現列印多重數組。
舉個例子:
int[][][] a=new int[3][4][5];
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
for(int k=0;k<5;k++){
a[i][j][k]=i*j*k;
}
}
}
System.out.println(Arrays.deepToString(a));
java設計人員之所以不為Array類設計toString是因為不好確定用什麼方式列印數組,是用「【」,還是用「{」,以及要不要「,」等都是問題。
Arrays的deepToString方法如下:

public static String deepToString(Object[] a) {
if (a == null)
return "null";

int bufLen = 20 * a.length;
if (a.length != 0 && bufLen <= 0)
bufLen = Integer.MAX_VALUE;
StringBuilder buf = new StringBuilder(bufLen);
deepToString(a, buf, new HashSet());
return buf.toString();
}

private static void deepToString(Object[] a, StringBuilder buf,
Set<Object[]> dejaVu) {
if (a == null) {
buf.append("null");
return;
}
dejaVu.add(a);
buf.append('[');
for (int i = 0; i < a.length; i++) {
if (i != 0)
buf.append(", ");

Object element = a[i];
if (element == null) {
buf.append("null");
} else {
Class eClass = element.getClass();

if (eClass.isArray()) {
if (eClass == byte[].class)
buf.append(toString((byte[]) element));
else if (eClass == short[].class)
buf.append(toString((short[]) element));
else if (eClass == int[].class)
buf.append(toString((int[]) element));
else if (eClass == long[].class)
buf.append(toString((long[]) element));
else if (eClass == char[].class)
buf.append(toString((char[]) element));
else if (eClass == float[].class)
buf.append(toString((float[]) element));
else if (eClass == double[].class)
buf.append(toString((double[]) element));
else if (eClass == boolean[].class)
buf.append(toString((boolean[]) element));
else { // element is an array of object references
if (dejaVu.contains(element))
buf.append("[...]");
else
deepToString((Object[]) element, buf, dejaVu);
}
} else { // element is non-null and not an array
buf.append(element.toString());
}
}
}
buf.append(']');
dejaVu.remove(a);
}
}
java.lang.reflect.Array類的內容如下,你如果有能力可以改改:
/*
* @(#)Object.java 1.73 06/03/30
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.lang;
public class Object {

private static native void registerNatives();
static {
registerNatives();
}

public final native Class<?> getClass();

public native int hashCode();

public boolean equals(Object obj) {
return (this == obj);
}

protected native Object clone() throws CloneNotSupportedException;

public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

public final native void notify();

public final native void notifyAll();

public final native void wait(long timeout) throws InterruptedException;

public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}

if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}

if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
timeout++;
}

wait(timeout);
}

public final void wait() throws InterruptedException {
wait(0);
}

protected void finalize() throws Throwable { }

⑹ Java如何用數組列印出一個平行四邊形

//Java如何用數組列印出一個平行四邊形
import java.util.Scanner;

public class Main2 {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入底長和高");
int i=scanner.nextInt();
int j=scanner.nextInt();
System.out.println("空心平行四邊形為:");
for(int k=j;k>0;k--){
(int p=k;p>0;p--)
System.out.print(" ");
System.out.print("*");
for(int q=i-2;q>0;q--){
if(k==j||k==1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("*");
}
System.out.println("實心平行四邊形為:");
for(int k=j;k>0;k--){
for(int p=k;p>0;p--)
System.out.print(" ");
System.out.print("*");
for(int q=i-2;q>0;q--){
if(k==j||k==1)
System.out.print("*");
else
System.out.print("*");
}
System.out.println("*");
}
}
}
輸出如下:
*****
* *
* *
*****
實心平行四邊形為:
*****
*****
*****
*****
如果要保存到數組,定義一個String [][]=new String[i][i+j-1];
將對應的行存入即可

⑺ 利用Java中字元串列印數組組合

珍藏的排列演算法,實現了個java版
public class Test{
static public void swapTwo(final int a[], final int k1, final int k2){
t=a[k1]; a[k1]=a[k2]; a[k2]=t;
}
static public void permutation(final int a[], final int level, final int n){
if(level==n){
for(int i=0;i<n;i++) System.out.print(a[i]+" ");
System.out.println();
}else for(int i=level;i<a.length;i++){
swapTwo(a, level, i);
permutation(a, level+1, n);
swapTwo(a, i, level);
}
}
public static void main(String[] args) {
int a[]={3,7,9};
permutation(a, 0, 3);
}
}
========
3 7 9
3 9 7
7 3 9
7 9 3
9 7 3
9 3 7

⑻ java數組怎麼輸出

1、循環數組

2、獲取數組元素,通過system.out.print輸出

示例:

String[]arr={"a","b"};//定義一個數組版
for(inti=0;i<arr.length;i++){//循環數組
System.out.println(arr[i]);//輸出數組元權素。
}

⑼ java 中如何輸出字元型數組

public class test {
public static void main(String[] args) {
char [] chars = {'d','g','z','d','e'};
/*for each輸出復,其中String類型變數s迭代數組中制的每一個元素,這是自動的,
也就是說第一次循環s的值是strs字元數組的第一個元素,
第二次則為第二個,以此類推。*/
for(char c: chars) {
System.out.println(c);
}
/*for循環輸出,不做過多解釋了。*/
for(int i = 0; i < chars.length; i ++) {
System.out.println(chars[i]);
}
}
}

}

閱讀全文

與java列印string數組相關的資料

熱點內容
win10快速訪問共享文件 瀏覽:259
喜馬拉雅電腦文件導出 瀏覽:615
js取商運算 瀏覽:719
幼兒編程貓是什麼 瀏覽:347
dnf哪個網站補丁 瀏覽:646
iphone自動關機能打通 瀏覽:794
怎麼連接伺服器資料庫 瀏覽:907
大數據時代羅輯思維 瀏覽:827
聯想y50pwin10開機速度 瀏覽:236
網路游戲對我們的身體有什麼壞處 瀏覽:950
電腦接觸不良文件 瀏覽:689
星成大海是哪個app可以看 瀏覽:418
施耐德plc編程軟體哪個好用 瀏覽:680
網路k歌什麼麥 瀏覽:653
java創建oracle存儲過程 瀏覽:84
iphone6越獄後不保修 瀏覽:302
app君生病了 瀏覽:256
抖音大數據處理中心在哪裡 瀏覽:668
5s的app在那裡 瀏覽:665
ssojava 瀏覽:282

友情鏈接