
在数据结构领域中,vector是特定对象的可增长的类数组。vector类属于遗留类,与集合完全兼容。在java.util包中,List接口可以使用这里列出的所有方法。这里的初始容量是10,一般方法是−
Vectorv = new Vector ();
compare()方法接受两个参数,然后使用Java环境逻辑进行比较。
对2D数组按左对角线排序的算法
Here is the particular algorithm to sort the 2D array across left diagonal.
-
第一步 - 开始。
立即学习“Java免费学习笔记(深入)”;
步骤 2 - 逐个遍历所有左对角线。
Step 3 − Add elements on that left diagonal in the vector.
第四步 - 处理这些向量。
第五步 - 再次进行排序。
Step 6 − Push them back from vector to left diagonal.
第7步 - 删除所有向量,使集合为空。
第8步 - 再次开始全新的排序。
第九步 - 再次重复所有步骤。
第10步 - 逐步完成所有左对角线。
第11步 - 终止进程。
在左对角线上对2D数组进行排序的语法
在这里,我们有一些特定的语法来沿左对角线拍摄一些2D数组,如下所示:
A. removeAll():
bee餐饮点餐外卖小程序是针对餐饮行业推出的一套完整的餐饮解决方案,实现了用户在线点餐下单、外卖、叫号排队、支付、配送等功能,完美的使餐饮行业更高效便捷!功能演示:1、桌号管理登录后台,左侧菜单 “桌号管理”,添加并管理你的桌号信息,添加以后在列表你将可以看到 ID 和 密钥,这两个数据用来生成桌子的二维码2、生成桌子二维码例如上面的ID为 308,密钥为 d3PiIY,那么现在去左侧菜单微信设置
Syntax:
Vector.removeAll(Vectors As Value) It is used to remove all the elements from the vector.
B. Collections.sort():
Syntax:
Collections.sort(Vectors As Value) This method is used to sort the vector in a process.
C. add():
Syntax:
Vector.add(Value as the integer value) It is used to add some elements in the vector.
D. get():
Syntax:
Vector.get(3); This method used to store the vector element at a pricular index.
在这些特定的语法中,我们尝试对一些2D数组进行沿左对角线排序。
对2D数组进行左对角线排序的方法
方法1 - Java程序对2D数组按照左对角线排序
方法2 - Java程序按对角线递减顺序对2D矩阵进行排序
方法3 - Java程序对2D矩阵进行对角线排序并获取其总和
Java程序:对2D数组按照左对角线进行排序
In this Java code we have tried to show how to sort a 2D array across the left diagonal in a general manner.
Example 1
的中文翻译为:示例1
import java.io.*;
import java.lang.*;
import java.util.*;
public class ARBRDD {
public static void main(String[] args)
throws java.lang.Exception{
int[][] arr = { { 5, 2, 0, 7, 1 }, { 3, 4, 2, 9, 14 },
{ 5, 1, 3, 5, 2 }, { 4, 2, 6, 2, 1 },
{ 0, 6, 3, 5, 1 }, { 1, 4, 7, 2, 8 } };
System.out.println("Matrix without sorting data is here ----> \n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
Vector v = new Vector<>();
for (int i = 0; i < 5; i++) {
v.add(arr[i][i]);
}
Collections.sort(v);
for (int j = 0; j < 5; j++) {
arr[j][j] = v.get(j);
}
System.out.println("Matrix after sorting data is here ----> \n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
输出
Matrix without sorting data is here ----> 5 2 0 7 1 3 4 2 9 14 5 1 3 5 2 4 2 6 2 1 0 6 3 5 1 Matrix after sorting data is here ----> 1 2 0 7 1 3 2 2 9 14 5 1 3 5 2 4 2 6 4 1 0 6 3 5 5
Java程序按对角线递减顺序对2D矩阵进行排序
在这段Java代码中,我们尝试展示如何以递减的方式对一个2D数组矩阵沿着左对角线进行排序。
Example 2
的中文翻译为:示例2
import java.io.*;
import java.util.*;
public class ARBRDD {
public static void
diagonalSort(ArrayList > mat){
int row = mat.size();
int col = mat.get(0).size();
ArrayList > Neg = new ArrayList >();
ArrayList > Pos = new ArrayList >();
int i, j;
for (i = 0; i < row; i++) {
ArrayList temp
= new ArrayList();
Neg.add(temp);
}
for (j = 0; j < col; j++) {
ArrayList temp
= new ArrayList();
Pos.add(temp);
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (j < i) {
Neg.get(i - j).add(mat.get(i).get(j));
}
else if (i < j) {
Pos.get(j - i).add(mat.get(i).get(j));
}
else {
Pos.get(0).add(mat.get(i).get(j));
}
}
}
for (i = 0; i < row; i++) {
Collections.sort(Neg.get(i));
;
}
for (i = 0; i < col; i++) {
Collections.sort(Pos.get(i));
;
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (j < i) {
int d = i - j;
int l = Neg.get(d).size();
mat.get(i).set(j,
Neg.get(d).get(l - 1));
Neg.get(d).remove(l - 1);
}
else if (i < j) {
int d = j - i;
int l = Pos.get(d).size();
mat.get(i).set(j,
Pos.get(d).get(l - 1));
Pos.get(d).remove(l - 1);
}
else {
int l = Pos.get(0).size();
mat.get(i).set(j,
Pos.get(0).get(l - 1));
Pos.get(0).remove(l - 1);
}
}
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
System.out.print(mat.get(i).get(j) + " ");
}
System.out.println();
}
}
public static void main(String[] args){
ArrayList > arr
= new ArrayList >();
ArrayList row1 = new ArrayList();
row1.add(10);
row1.add(2);
row1.add(3);
arr.add(row1);
ArrayList row2 = new ArrayList();
row2.add(4);
row2.add(5);
row2.add(6);
arr.add(row2);
ArrayList row3 = new ArrayList();
row3.add(7);
row3.add(8);
row3.add(9);
arr.add(row3);
diagonalSort(arr);
}
}
输出
10 6 3 8 9 2 7 4 5
Java程序对2D矩阵进行对角线排序并获取其总和
在这段Java代码中,我们尝试展示如何对一个2D数组矩阵沿着左对角线进行排序,并得到其总和。
Example 3
import java.util.*;
public class ARBRDD{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int i,j,row,col,sum=0;
System.out.println("Enter the number of rows ---->:");
row = sc.nextInt();
System.out.println("Enter the number of columns---->:");
col = sc.nextInt();
int[][] mat = new int[row][col];
System.out.println("Enter the elements of the matrix: -----@") ;
for(i=0;i
相关文章
如何正确对二维数组进行升序排序并打印
标题:使用 AspectJ 实现基于字段注解的读写访问拦截教程
如何使用Java开发密码校验程序_Java字符串与正则实战说明
在Java中什么是线程安全_Java并发安全核心概念解析
专门学Java的网站_专注于Java教学的垂直学习网站
java速学教程(入门到精通)
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
下载
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn









