21 Sept 2012

Converting Object into integer in java


// get array
Object ia[] = al.toArray();
int sum = 0;
// sum the array
for(int i=0; i<ia.length; i++)
sum += ((Integer) ia[i]).intValue();
System.out.println("Sum is: " + sum);
}
}

The output from the program is shown here:

Contents of al: [1, 2, 3, 4]
Sum is: 10

0 comments:

Post a Comment