How to print 2D array elements without using nested loops