java怎么删除数组中一列数据

有四种方法可以删除 Java 数组中的一列数据:使用 System.arraycopy() 复制数组的每一行,跳过要删除的列。使用 Guava 库遍历每一行并过滤掉要删除的列。使用 Apache Commons Lang 库直接移除列。手动遍历数组并重新构造一个新数组,排除要删除的列。

java怎么删除数组中一列数据

如何删除 Java 数组中一列数据

要删除 Java 数组中一列数据,有几种方法:

1. 使用 System.arraycopy()

int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };int[][] newArr = new int[arr.length - 1][];// 复制数组的每一行,跳过要删除的列for (int i = 0; i < arr.length; i++) {    newArr[i] = new int[arr[i].length - 1];    System.arraycopy(arr[i], 0, newArr[i], 0, arr[i].length - 1);}

2. 使用 Guava 库

阿里云-虚拟数字人 阿里云-虚拟数字人

阿里云-虚拟数字人是什么? …

阿里云-虚拟数字人 2 查看详情 阿里云-虚拟数字人

立即学习“Java免费学习笔记(深入)”;

import com.google.common.collect.ImmutableList;import com.google.common.collect.ImmutableList.Builder;int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };Builder<ImmutableList<ImmutableList>> builder = ImmutableList.builder();// 遍历每一行并过滤掉要删除的列for (ImmutableList row : ImmutableList.copyOf(arr)) {    builder.add(ImmutableList.copyOf(row.subList(0, row.size() - 1)));}ImmutableList<ImmutableList> newArr = builder.build();

3. 使用 Apache Commons Lang 库

import org.apache.commons.lang3.ArrayUtils;int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };int[][] newArr = ArrayUtils.removeColumns(arr, 1);

4. 手动遍历并重构数组

int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };int[][] newArr = new int[arr.length][arr[0].length - 1];// 遍历每一行并创建新数组for (int i = 0; i < arr.length; i++) {    for (int j = 0; j < newArr[i].length; j++) {        if (j < arr[i].length - 1) {            newArr[i][j] = arr[i][j];        }    }}

以上就是java怎么删除数组中一列数据的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/620929.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月11日 06:28:45
下一篇 2025年11月11日 06:29:35

相关推荐

发表回复

登录后才能评论
关注微信