user3553370
user3553370

Reputation: 67

Add value to arrayList in android

I'm trying to add int values to array list

ArrayList<Integer> arraylistPage1;

for(int j=1;j<=totalPage;j++){  
     int x=0;
     for(int i=1;i<=rangeMode;i++){
          if(binaryTable[i][j]==1){
             cardInPage[j][x]=i;
             System.out.println("card number: "+i);
             arraylistPage1.add(i);
             System.out.println(arraylistPage1);
             x++;                   
          }
     }
     System.out.println("page-"+j+" total card:"+x);
     totalCardInPage[j]=x;
}

But when I run it, the program just stops.

Does anyone know how to fix this?

Thanks

Upvotes: 0

Views: 82

Answers (1)

Sunny Garg
Sunny Garg

Reputation: 1073

Change the code to this,

ArrayList<Integer> arraylistPage1;

for(int j=1;j<=totalPage;j++){  
 int x=0;
 arraylistPage1 = new ArrayList<Integer>(rangeMode);
 for(int i=1;i<=rangeMode;i++){
      if(binaryTable[i][j]==1){
         cardInPage[j][x]=i;
         System.out.println("card number: "+i);
         arraylistPage1.add(i);
         System.out.println(arraylistPage1);
         x++;                   
      }
 }
 System.out.println("page-"+j+" total card:"+x);
 totalCardInPage[j]=x;
}

Upvotes: 1

Related Questions