简体   繁体   English

将数组分为两个不同的数组

[英]Dividing array into two different arrays

I have made 2-D array to store patterns[user defined] and it's features [user can decide No of features for each pattern].I have to divide this structure into two parts TR1 and TR2 such that half features for each pattern goes in TR1 and remaining half goes in TR2. 我已经制作了一个二维数组来存储模式[用户定义],它的特征是[用户可以决定每个模式的特征数]。我必须将此结构分为两部分TR1和TR2,以便每个模式的一半特征都进入TR1和其余的一半进入TR2。 Code for the same 相同的代码

 int n;//No of Patterns
 int f=0;;//No of Features


 System.out.println("Enter No of Patterns:");
 n=Integer.parseInt(input.readLine());

 //2-D array to store Features
 int pattern[][]= new int[n][20];
 int tr1[][]=new int[n][20];//TR1
 int tr2[][]=new int[n][20];//TR2

//No of Features for each Pattern
 for(int i=0;i<n;i++)//NO of Features for each Pattern
 { 
     System.out.println("Enter No of Features for Pattern "+(i+1)+" : ");
     f=Integer.parseInt(input.readLine());
     pattern[i]=new int[f];
     tr1[i]=new int[f/2];//definining size of TR1 
     tr2[i]=new int[f/2];//definining size of TR2
 }

//Features of each pattern
for(int i=0;i<n;i++)
 {
    System.out.println("Enter Features for Pattern "+(i+1)+" : ");
    for(int j=0;j<pattern[i].length;j++)
    {
    pattern[i][j]=Integer.parseInt(input.readLine());

    }
 }
//Split into TR1 & TR2 
for(int i=0;i<n;i++)
 {
    for(int j=0;j<pattern[i].length/2;j++)
    {
    tr1[i][j]=pattern[i][j];
    }   
    int x=0;//Fill TR2 from 0 index
    for(int j=pattern[i].length;j<pattern[i].length;j++)
    {
    tr2[i][x]=pattern[i][j];
            x++;
    }
 }




//Print Features of each pattern
for(int i=0;i<n;i++)
 {

    for(int j=0;j<pattern[i].length;j++)
    {
    System.out.print(" "+pattern[i][j]+" ");
    }
    System.out.println();
 }

 //Print TR1
 for(int i=0;i<n;i++)
 {

    for(int j=0;j<tr1[i].length;j++)
    {
    System.out.print(" "+tr1[i][j]+" ");

    }
    System.out.println();
 }

     //Print TR2
 for(int i=0;i<n;i++)
 {

    for(int j=0;j<tr2[i].length;j++)
    {
    System.out.print(" "+tr2[i][j]+" ");

    }
    System.out.println();
 }

The issue is all the values in TR2 become 0 in output.But TR1 output is correct. 问题是TR2中的所有值在输出中都变为0,但是TR1输出正确。

In the for loop the condition, j < pattern[i].length is false because: for循环中,条件j < pattern[i].length为false,因为:

int j = pattern[i].length;

Since the condition is false for the first time the statements inside for loop is not executing at all.That's why the elements of TR2 remains uninitialized that's why elements of TR2 becomes 0 . 由于条件第一次为假,因此for循环内的语句根本不执行,这就是TR2的元素保持未初始化的原因,这就是TR2的元素变为0的原因。 .

for(int j=pattern[i].length;j<pattern[i].length;j++)
{
  tr2[i][x]=pattern[i][j];  //Never executes
  x++;                      //Never Executes 
}

Checkout the link it'll help. 检出链接会有所帮助。 java-how-to-split-a-2d-array-into-two-2d-arrays java如何将一个2d数组拆分为两个2d数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中的两个不同 arrays 之间除数 - Dividing numbers between two different arrays in Java 在java中将整数数组划分为不同大小的数组 - Dividing an integer array into different sized arrays in java 两种不同类型的阵列进入第一个阵列 - Two Different types of Arrays into the first Array 来自两个不同数组的JAVA数组串联 - JAVA array concatenation from two different arrays Java如何将数组拆分为两个不同的数组,然后在数组中添加一个数字 - Java How to split array into two different arrays, and then add a number to the arrays 通过连续除以 2 对数组求和的不同方法 - Different ways of summing an array by continuously dividing by 2 在没有除法运算符的情况下将数组分为两个 - dividing an array into two without a division operator 对存储在布尔数组中的两个二进制数进行加,减,除和乘运算 - Adding, Subtracting, Dividing, and Multiplying two binary numbers stored in boolean arrays Java:将单个数组除以对象元素值分成2个数组 - Java: Dividing a single array by a object element value in to 2 arrays 将两个二维 Arrays 变成一个二维数组,不同长度的 Arrays 和不同长度的 Arrays inside 2D Array - Turning two 2-Dimensional Arrays into one 2-Dimensional Array with different length of Arrays and different length of Arrays inside 2D Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM