您的位置:首页 > 其它

关于数组的常用方法01

2010-05-29 19:18 141 查看
package mytools.arraytool;

public class ArrayUtil {
/**
* 此个方法用于打印一个一维int类型的数组
*
* @param x
* 是要打印的int类型数组
*/
public static void oneintprint(int x[]) {
for (int y = 0; y < x.length; y++) {
System.out.print("数组[" + y + "] = " + x[y] + "/t");
}
System.out.println("");
}

/**
* 此个方法用于打印一个二维int类型的数组
*
* @param x
* 是要打印的int类型数组
*/
public static void twointprint(int a[][]) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + "/t");
}
System.out.println("");
}
}

/**
* 此个方法用于初始化一个一维int类型的数组
*
* @param len
* 代表数组的长度,你可以指定它的长度
* @return 一个int类型的数组,数组初始化为1 2 3 4 5 6 7 8 9.......
*/
public static int[] init(int len) {
// java中的数组是可以动态开辟的
int x[] = new int[len];
// 进行数组的初始化
for (int y = 0; y < x.length; y++) {
x[y] = y + 1;
}
return x;
}

/**
* 此个方法用于复制一个数组
*/
public static void copyArray() {
int[] src = { 1, 3, 10, 6, 7, 8 };
int[] dest = new int[6];

System.arraycopy(src, 0, dest, 0, 6);
ArrayUtil.oneintprint(dest);
}

/**
* 数组的声明 创建实例 可以参考
*/
public static void arraydeclare() {
int source[] = { 1, 2, 3, 4, 5, 6 };// 这是一维数组静态声明方式
int x[] = new int[10];// 这是一维数组动态声明方式
int a[][] = { { 1, 3 }, { 2, 3, 4, 5 }, { 2, 3, 4 } };//这是二维数组静态声明方式
int c[][]= new int[3][7];//这是二维数组动态声明方式
int b[][]= new int[3][];//这是二维数组动态声明方式
b[0]=new int[2];
b[0]=new int[3];
b[0]=new int[5];
// 数组的使用看的是下标
// 0 1 2 3 4 5 6 7 8 9
// 如果要打印第1个数据
System.out.println(source[0]);
}

public static void main(String args[]) {
int a[][] = { { 1, 3 }, { 2, 3, 4, 5 }, { 2, 3, 4 } };
twointprint(a);
int source[] = { 1, 2, 3, 4, 5, 6 };
oneintprint(source);
ArrayUtil.copyArray();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: