導航:首頁 > 編程語言 > java一維數組轉二維數組

java一維數組轉二維數組

發布時間:2022-05-05 11:01:07

java中怎麼把倆個一維數組合成二維數組輸出

當然可以,可以設置一個外循環,裡面設置兩個內循環,判斷奇數的時候輸出第一個一維數組元素,然後偶數的時候輸出第二個一維數組的元素。

㈡ java一維數組根據第一個數轉二維數組

public static void main(String[] args) {
String[] array = {"11AAA","11BBB","22CCC","33DDD","33EEE"};
String[][] convertArray = new String[array.length][0];
for (String str : array) {
int _index = Integer.valueOf(String.valueOf(str.charAt(0)));
convertArray[_index] = Arrays.Of(convertArray[_index], convertArray[_index].length+1);
convertArray[_index][convertArray[_index].length-1] = str.substring(1);
}
for (int i = 0; i < convertArray.length; i++) {
if(convertArray[i] != null && convertArray[i].length>0)System.out.print(i+"->");
for (int j = 0; j < convertArray[i].length; j++) {
System.out.print(convertArray[i][j]);
System.out.print(" ");
}
if(convertArray[i] != null && convertArray[i].length>0)System.out.println();
}
}
//執行結果
1->1AAA 1BBB
2->2CCC
3->3DDD 3EEE

㈢ java怎樣將一維字元串數組看成二維數組

package test;

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) throws Exception {
String[] str = new String[] { "abkdkllslk", "122j2aakkd",
"2k2kk2kskks", "28282871787", "kwkkks" };
char[][] ch = new char[str.length][];// 轉換成二維字元數組
for (int i = 0; i < str.length; i++) {
ch[i] = str[i].toCharArray();
}
Map<String, Integer> m = new LinkedHashMap<String, Integer>();
for (char[] cs : ch) {
for (char c : cs) {
String key = String.valueOf(c);
if (m.containsKey(key)) {
m.put(key, m.get(key) + 1);
} else {
m.put(key, 1);
}
}
}
System.out.println(m);
}
}
//兄弟記得給分,好不容易給你敲出來的。


㈣ 一維數組變成二維數組 java

public class Test {
public static void main(String[] args) {
String str[]={"a b c","1 2 3","A B C"};//聲明一個一維數組
String s[][]=new String[str.length][];//聲明一個二維數組
for(int i=0;i<str.length;i++){
s[i]=str[i].split(" ");//按照空格拆分字元串

}
for(int i=0;i<s.length;i++){
for(int j=0;j<s[i].length;j++) {
System.out.println(s[i][j]);//將二維數組裡面的東西全部輸出
}
}
}
}

㈤ java中如何將一維數組的內容復制到二維數組

int n = 0;
for(int i = 0; i<height; i++){
for(int j = 0; j< width; j++){
b[i][j]=a[n];
n++;
}
}
OK!height和width是你的2d數組的兩個值!

㈥ JAVA一維數組變二維數組問題

下面這個程序做到了:

public class Test1 {
public static void main(String[] args) {
int a [] = new int[20];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}

int n = 5;//列
int m = a.length / n;//行
int[][] b = new int[m][n];
int num = -1;

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
num++;
b[i][j] = a[num];
}
}

System.out.println("原來的一維數組是:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}

System.out.println();
System.out.println("轉換後的二維數組是:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}

㈦ java 一維數組 怎麼變二維數組

下面這個程序做到了:

public class Test1 {
public static void main(String[] args) {
int a [] = new int[20];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}

int n = 5;//列
int m = a.length / n;//行
int[][] b = new int[m][n];
int num = -1;

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
num++;
b[i][j] = a[num];
}
}

System.out.println("原來的一維數組是:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}

System.out.println();
System.out.println("轉換後的二維數組是:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}

㈧ java中怎麼把一維數組轉換成二維數組

轉換是個什麼意思,高階轉換低階可以,低階轉化高階需要規則

㈨ 如何用JAVA把一維數組拆分為二維數組

關鍵是要看你想怎麼拆,如一維數組長20,二維數組4*5,可以按行優先也可以按列優先拆分,如果按行優先,則順序每次從一維數組讀取4個值賦給二維數組的一行,
int[] a = {3,..4};//共20個
int[][5] b = new int[4][5];
for(int i=0;i<4;i++){
b[i] = new int[5];
for(int j=0;j<5;j++){
b[i][j] = a[i*5+j];
}
}

㈩ java中怎麼把一維數組的值一個個賦給另一個二維數組

利用 for循環遍歷數組 即可

for循環是開界的。它的一般形式為: for(; <條件表達式>; ) 語句; 初始化總是一個賦值語句, 它用來給循環控制變數賦初值; 條件表達式是一個關系表達式, 它決定什麼時候退出循環; 增量定義循環控制變數每循環一次後 按什麼方式變化。這三個部分之間用";"分開。 例如: for(i=1; i<10時, 結束循環。

for語句比while語句和do-while都要靈活,是一種功能更大、更常用的循環語句,它的一般語法格式為:

for(表達式1;表達式2;表達式3)

{

循環體

}

其中,表示式可以省略,但是分號不可省略。

閱讀全文

與java一維數組轉二維數組相關的資料

熱點內容
cnc編程前景怎麼樣 瀏覽:319
lniux命令詳解 瀏覽:493
linuxmysql查詢日誌 瀏覽:368
老捷達夥伴壓縮比 瀏覽:93
改後綴加密 瀏覽:432
郵局選址問題演算法 瀏覽:14
河北伺服器內存雲主機 瀏覽:12
在電腦上怎麼找到加密狗圖標 瀏覽:435
電腦的瀏覽器怎麼打開pdf文件怎麼打開 瀏覽:142
pdf卡片庫下載 瀏覽:11
單片機中二進製表示什麼 瀏覽:725
java網路編程推薦 瀏覽:794
施耐德開關編程 瀏覽:65
組織胚胎學pdf 瀏覽:844
linux查看發包 瀏覽:496
加密貨幣交易所暴利時代 瀏覽:824
歌詞滾動效果android 瀏覽:14
程序員一天的六場戰斗 瀏覽:797
自製壓縮泵的做法 瀏覽:622
androidstring變數 瀏覽:247