通过给出正数使用递归在 java 中构建二维数组
build 2d array in java using recrursion by giving a positive number
我有一个程序通过给出一个正数来构建二维数组,并以递归方式显示结果,如下例所示:
n = 6 ...其中 n 是正数 nbumber
结果将显示此二维数组:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
谁能帮我得到这个结果??
这是我的代码:
public class positiveNumber{
public static void main(String[] args){
for(int i = 0 i<= 6 i++){
{
for(int j = 0 j <= 6 j++){
if(i == j){
{
}
}
}
}
尝试在 list 中添加数字并在每次迭代中以相反的顺序打印列表
public class positiveNumber{
public static void main(String[] args){
int n= taking Input from user;
List<Integer> listOfNumbers=new ArrayList<Integer>();
for(int i = 0 ; i< n ; i++){
listOfNumbers.add(i);
printListInReverse(listOfNumbers);
}
}
public static void printListInReverse(List<Integer>listOfNumbers){
List temp = new ArrayList(listOfNumbers);
Collections.reverse(temp);
System.out.println(temp); //either iterate the list or print in once
}
}
输出
[0]
[1, 0]
[2, 1, 0]
[3, 2, 1, 0]
[4, 3, 2, 1, 0]
[5, 4, 3, 2, 1, 0]
如果你想删除这些 [] 括号然后而不是直接打印列表使用 Iterator 迭代列表然后打印列表中的每个元素
更新
具有递归
的相同程序
public class positiveNumber{
static List<Integer> listOfNumbers=new ArrayList<Integer>();
static int n;
public static void main(String[] args){
n= "take input fmo user"
print(0);
}
/*
*Using Recursion
*/
public static void print(int i){
if(i==n)
return;
listOfNumbers.add(i);
printListInReverse();
print(i+1);
}
public static void printListInReverse(){
List temp = new ArrayList(listOfNumbers);
Collections.reverse(temp);
System.out.println(temp); //either iterate the list or print in once
}
}
试试这样的东西:
public static void main(String args[]) {
int[][] numbers = new int[6][6];
displayPyramid(6, numbers);
System.out.println("2d array is");
for (int i = 0; i < 6; i++)
System.out.println(Arrays.toString(numbers[i]));
}
private static void displayPyramid(int next, int[][] numbers) {
if (next == 0) {
return;
}
StringBuilder sb = new StringBuilder();
for (int i = numbers[0].length - next, k = 0; i >= 0; i--) {
numbers[numbers[0].length - next][k++] = i;
sb.append(i);
sb.append(" ");
}
System.out.println(sb);
displayPyramid(next - 1, numbers);
}
Output:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
2d array is
[0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0]
[2, 1, 0, 0, 0, 0]
[3, 2, 1, 0, 0, 0]
[4, 3, 2, 1, 0, 0]
[5, 4, 3, 2, 1, 0]
我有一个程序通过给出一个正数来构建二维数组,并以递归方式显示结果,如下例所示:
n = 6 ...其中 n 是正数 nbumber
结果将显示此二维数组:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
谁能帮我得到这个结果??
这是我的代码:
public class positiveNumber{
public static void main(String[] args){
for(int i = 0 i<= 6 i++){
{
for(int j = 0 j <= 6 j++){
if(i == j){
{
}
}
}
}
尝试在 list 中添加数字并在每次迭代中以相反的顺序打印列表
public class positiveNumber{
public static void main(String[] args){
int n= taking Input from user;
List<Integer> listOfNumbers=new ArrayList<Integer>();
for(int i = 0 ; i< n ; i++){
listOfNumbers.add(i);
printListInReverse(listOfNumbers);
}
}
public static void printListInReverse(List<Integer>listOfNumbers){
List temp = new ArrayList(listOfNumbers);
Collections.reverse(temp);
System.out.println(temp); //either iterate the list or print in once
}
}
输出
[0]
[1, 0]
[2, 1, 0]
[3, 2, 1, 0]
[4, 3, 2, 1, 0]
[5, 4, 3, 2, 1, 0]
如果你想删除这些 [] 括号然后而不是直接打印列表使用 Iterator 迭代列表然后打印列表中的每个元素
更新
具有递归
的相同程序public class positiveNumber{
static List<Integer> listOfNumbers=new ArrayList<Integer>();
static int n;
public static void main(String[] args){
n= "take input fmo user"
print(0);
}
/*
*Using Recursion
*/
public static void print(int i){
if(i==n)
return;
listOfNumbers.add(i);
printListInReverse();
print(i+1);
}
public static void printListInReverse(){
List temp = new ArrayList(listOfNumbers);
Collections.reverse(temp);
System.out.println(temp); //either iterate the list or print in once
}
}
试试这样的东西:
public static void main(String args[]) {
int[][] numbers = new int[6][6];
displayPyramid(6, numbers);
System.out.println("2d array is");
for (int i = 0; i < 6; i++)
System.out.println(Arrays.toString(numbers[i]));
}
private static void displayPyramid(int next, int[][] numbers) {
if (next == 0) {
return;
}
StringBuilder sb = new StringBuilder();
for (int i = numbers[0].length - next, k = 0; i >= 0; i--) {
numbers[numbers[0].length - next][k++] = i;
sb.append(i);
sb.append(" ");
}
System.out.println(sb);
displayPyramid(next - 1, numbers);
}
Output:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
2d array is
[0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0]
[2, 1, 0, 0, 0, 0]
[3, 2, 1, 0, 0, 0]
[4, 3, 2, 1, 0, 0]
[5, 4, 3, 2, 1, 0]