关于数组的常用方法01

本文介绍了一个Java工具类,该类包含多个实用方法用于处理一维及二维int数组,包括打印、初始化和复制等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值