根据特定元素拆分java数组
本例中,要求根据元素85将数组拆分成多个新数组。为此,可以使用以下过程:
以下java代码实现了这个过程:
import java.util.arraylist; import java.util.list; public class arraysplitter { public static void main(string[] args) { int[] arr = {85, -86, 13, 2, 99, 99, 99, 99, 98, 98, 99, 99, 99, 99, 20, 85, -86, 13, 2, 99, 99, 99, 99, 99, 99, 99, 85, 12, 85, -86, 13, 2, 99}; splitarray(arr, 85); } public static void splitarray(int[] arr, int target) { list<list<integer>> result = new arraylist<>(); list<integer> temp = new arraylist<>(); for (int i : arr) { if (i == target) { if (!temp.isempty()) { result.add(new arraylist<>(temp)); temp.clear(); } temp.add(i); } else { temp.add(i); } } result.add(temp); for (list<integer> list : result) { system.out.println(list); } } }
输出:
立即学习“Java免费学习笔记(深入)”;
[85, -86, 13, 2, 99, 99, 99, 99, 98, 98, 99, 99, 99, 99, 20] [85, -86, 13, 2, 99, 99, 99, 99, 99, 99, 99] [85, 12] [85, -86, 13, 2, 99]
以上就是如何根据特定元素(例如数字85)将Java数组拆分成多个新数组?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号