23个回答

1.二维数组的方式
2.Vector(不定长数组)嵌套的方式
3.Vector内套数组
3.List>的方式
4.List>的方式
……

1.二维数组

`````` // 创建定长二维数组
public static int[][] getArrays(){
// 创建二维数组，2 -- 表示外层数组的长度，3表示里层数组的长度
int[][] a = new int[2][3];
int[] b = {1, 2, 3};
int[] c = {4, 5, 6};
a[0] = b;
a[1] = c;
return a;
}

// 二维数组
int[][] a = getArrays();
for(int i = 0; i < a.length; i++){
int[] b = a[i];
for(int j = 0; j < b.length; j++){
System.out.print(b[j] + " ");
}
System.out.println(" ");
}

System.out.println("\n\n");
``````

2.Vector内套数组的方式

`````` // 外层使用不定长数组
public static Vector<int[]> getVectors(){
Vector<int[]> vector = new Vector<>();
int[] b = {1, 2, 3};
int[] c = {4, 5, 6};
return vector;
}

// vector
Vector<int[]> vector = getVectors();
// 获取迭代器
Iterator<int[]> iterator = vector.iterator();
while (iterator.hasNext()) {
int[] b = iterator.next();
for(int c : b){
System.out.print(c + " ");
}
System.out.println(" ");
}
``````

class内部调用的话为何要return 2个数组，完全可以在class下面声明全局变量 a[]=null，b[]=null；然后再方法内部给a,b附值就好了。

package test;

public class GetData {
public static Data setData() {
int a[]={'1','2'};
int b[]={'3','5','7'};
Data data=new Data();
data.setA(a);
data.setB(b);
return data;
}
public static class Data{
int a[]=null;
int b[]=null;
public int[] getA() {
return a;
}
public void setA(int[] a) {
this.a = a;
}
public int[] getB() {
return b;
}
public void setB(int[] b) {
this.b = b;
}

``````}
``````

}
java的原则就是面向对象，

1.二维数组
2.List<>
3.map

```import java.util.ArrayList;
import java.util.List;

public class Demo {
public static void main(String[] args) {
List list = arrMethod();
String[] arr = (String[])list.get(0);
System.out.println(arr[1]);
}
public static List arrMethod() {
List list = new ArrayList();
String[] arr1 = new String[2];
arr1[0] = "str1";
arr1[1] = "str2";
String[] arr2 = new String[2];
arr1[0] = "str3";
arr1[1] = "str4";
return list;
}
}

``````
``````

package com.csdn.qestion;

import java.util.List;
import java.util.ArrayList;

public class CsdnQestion1 {
public static List getTwoArrays() {
String[] array1 = { "one", "two", "three" };
Integer[] array2 = { 1, 2, 3 };
List list = new ArrayList();
return list;
}

``````public static void main(String[] args) {
List<Object[]> list = getTwoArrays();
String[] array1 = new String[256];
Integer[] array2 = new Integer[256];
for (int i = 0; i < list.size(); i++) {

if (array1.getClass().getComponentType().getName() == list.get(i)
.getClass().getComponentType().getName()) {
array1 = (String[]) list.get(i);
}

if (array2.getClass().getComponentType().getName() == list.get(i)
.getClass().getComponentType().getName()) {
array2 = (Integer[]) list.get(i);
}
}
for (Integer integer : array2) {
System.out.println(integer);
}
}
``````

}