Java如何将二维数组转化为一维数组
作者:KFC宅急送专员 时间:2022-11-18 12:23:18
将二维数组转化为一维数组
1. 为了偷懒所以我写了一个随机生成二维数组的函数
/*
* 自动创建随机为100以内的二维数组: int nums[x][y]
* */
public static int[][] CreatedDemArray(int x,int y){
int nums[][]=new int[x][y];
for(int i=0;i<nums.length;i++){
for(int j=0;j<nums[i].length;j++){
nums[i][j]=(int)(Math.random()*100);
}
}
return nums;
}
2. 定义一个5x8的二维函数
public static void main(String[] args) {
createdarray ca=new createdarray();
int nums[][]=CreatedDemArray(5,8);
}
3. 定义一个长度为40 的int 数组,并使用System.arraycopy的方式将二维数组填充到一维数组temp中
public static void main(String[] args) {
createdarray ca=new createdarray();
int nums[][]=CreatedDemArray(5,8);
//定义整数数组
int[] temp=new int[40];
//通过 arraycopy方法来将二维数组填充到一维数组temp中
for(int i=0;i<nums.length;i++){
System.arraycopy(nums[i],0,temp,i*8,8);
}
}
4. 如果填充到一维数组之后我想搞点事,把一维数组进行排序后,回填到二维数组中
public static void main(String[] args) {
createdarray ca=new createdarray();
int nums[][]=CreatedDemArray(5,8);
int[] temp=new int[40];
for(int i=0;i<nums.length;i++){
System.arraycopy(nums[i],0,temp,i*8,8);
}
// 对一维数组进行排序
Arrays.sort(temp);
// 将排序后的数组进行回填
int index=0;
while (index<temp.length){
for(int i=0;i<nums.length;i++){
for(int j=0;j<nums[i].length;j++){
nums[i][j]=temp[index++];
}
}
}
System.out.println("排序后的数组如下");
// 这里因为懒所以我自定义了一个函数 打印二维数组
ShowDemArray(nums);
}
5. 补充一下上面用到的打印二维数组
/*
* 输出二维数组
* 这里我不知道怎么写我还去问了我前任:) 因为我实在不知怎么传这个参
* 如果要做成通用的比如说既能打印整数类型又能打印字符串类型的二维数组需要用的泛型...
* 毕业2年我已经忘了泛型怎么用,下次再补补
*/
public static void ShowDemArray(int[][] nums){
for (int i=0;i<nums.length;i++){
for (int j=0;j<nums[i].length;j++){
System.out.print(nums[i][j]+"\t");
if((j+1)%nums[i].length==0){
System.out.println();
}
}
}
}
今天也是想当测开的一天
一维数组转化为二维数组
由于经常在使用矩阵进行计算时,会首先将一维数组转为二维数组。因此,在这里记录一下,也希望对他人有帮助。
package deal;
/*
* author:合肥工业大学 管院学院 钱洋
*1563178220@qq.com
*/
public class ArryTest {
public static void main(String[] args) {
//创建一个一维数组 0,1,2,3...,10
double [] c= new double[10];
for (int i = 0; i < c.length; i++) {
c[i]=i;
}
double[][] testArr=TwoArry(c);
for (int i = 0; i < testArr.length; i++) {
for (int j = 0; j < testArr[i].length; j++) {
System.out.println(testArr[i][j]);
}
}
}
//一维数组转化为二维数组
public static double[][] TwoArry(double[] onedouble){
double[][] arr=new double[1][onedouble.length];
for (int i = 0; i < onedouble.length; i++) {
arr[0][i]=onedouble[i];
}
return arr;
}
}
程序运行结果。
来源:https://blog.csdn.net/wutrg1502/article/details/120045670
标签:Java,二维数组,一维数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring的xml文件打开没有namespace等操作选项的解决方案
2022-09-10 14:56:47
![](https://img.aspxhome.com/file/2023/4/66514_0s.jpg)
SpringMVC中Controller类数据响应的方法
2021-10-07 21:00:08
![](https://img.aspxhome.com/file/2023/0/75710_0s.png)
C#利用ZXing.Net生成条形码和二维码
2023-11-04 00:36:02
![](https://img.aspxhome.com/file/2023/9/79089_0s.png)
C#实现FFT(递归法)的示例代码
2022-12-30 05:21:06
![](https://img.aspxhome.com/file/2023/1/116611_0s.png)
Java用 Rhino/Nashorn 代替第三方 JSON 转换库
2023-11-04 02:20:26
Kotlin伴随对象的初始化方法示例讲解
2022-07-24 05:53:06
SpringBoot路径映射实现过程图解
2023-11-13 04:01:11
![](https://img.aspxhome.com/file/2023/6/59376_0s.png)
Java基础之方法重写详解
2022-05-15 20:11:04
Java创建线程的五种写法总结
2023-01-10 04:39:34
Spring入门实战之Profile详解
2021-10-03 19:08:34
![](https://img.aspxhome.com/file/2023/4/116184_0s.png)
Java源码解析之平衡二叉树
2023-11-29 11:16:40
![](https://img.aspxhome.com/file/2023/2/60532_0s.png)
详解SpringCloud Zuul过滤器返回值拦截
2023-02-05 07:59:37
100行C#代码实现经典扫雷游戏
2023-12-05 16:33:30
![](https://img.aspxhome.com/file/2023/0/70400_0s.png)
Android基于高德地图完全自定义Marker的实现方法
2023-03-09 03:32:30
![](https://img.aspxhome.com/file/2023/8/138618_0s.jpg)
Android 实现自定义圆形listview功能的实例代码
2022-06-20 06:58:29
![](https://img.aspxhome.com/file/2023/4/125434_0s.jpg)
C# 定时器定时更新的简单实例
2023-01-08 12:45:50
解决CollectionUtils.isNotEmpty()不存在的问题
2021-11-15 12:03:17
Java内存模型与JVM运行时数据区的区别详解
2023-11-24 13:29:08
java创建线程的两种方法区别
2023-11-11 09:17:52
android中Activity横竖屏切换的那些事
2023-04-02 22:32:33
![](https://img.aspxhome.com/file/2023/7/126787_0s.jpg)