fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main( String[] args )
  11. {
  12. System.out.println( "Running Shifting array..." );
  13.  
  14. int[][] pattern = new int[][]{
  15. { 1, 1, 1, 1, 1, 1, 1 },
  16. { 1, 2, 0, 0, 0, 2, 1 },
  17. { 1, 0, 3, 0, 3, 0, 1 },
  18. { 1, 0, 0, 4, 0, 0, 1 },
  19. { 1, 0, 3, 0, 3, 0, 1 },
  20. { 1, 2, 0, 0, 0, 2, 1 },
  21. { 1, 1, 1, 1, 1, 1, 1 },
  22. };
  23.  
  24. List<Integer[]> output = twoDArrayList( 2, pattern );
  25.  
  26. Iterator it = output.iterator();
  27. int count = 0;
  28. while( it.hasNext() )
  29. {
  30. Integer[] intarray = (Integer[]) it.next();
  31. System.out.print( "Array no. " + count + " in the list is : ");
  32. for( Integer i : intarray )
  33. {
  34. System.out.print( i + " ");
  35. }
  36. System.out.println();
  37. count++;
  38. }
  39. }
  40.  
  41. public static List<Integer[]> twoDArrayList(int shift, int[][] input)
  42. {
  43.  
  44. List<Integer[]> output = new ArrayList<Integer[]>();
  45. if( input.length == 0 ) return null;
  46. int columnlength = input.length;
  47. int rowlength = input[0].length;
  48. if (columnlength != rowlength) return null;
  49.  
  50. int padsize = shift;
  51. for( int i = 0; i < padsize; i++ )
  52. {
  53. Integer[] zeroes = new Integer[shift+columnlength];
  54.  
  55. for( int j = 0; j < shift+columnlength; j++)
  56. {
  57. zeroes[j] = 0;
  58. }
  59. output.add( zeroes );
  60. }
  61.  
  62. for( int i = 0; i < columnlength; i++ )
  63. {
  64. int[] row = input[i];
  65. int[] zeroes = new int[shift];
  66. List<Integer> temp = new ArrayList<Integer>();
  67. for( int j = 0; j < shift; j++)
  68. {
  69. temp.add(0);
  70. }
  71. for( int k = 0; k < row.length; k++)
  72. {
  73. temp.add(row[k]);
  74. }
  75. output.add(temp.toArray(new Integer[]{}));
  76. }
  77.  
  78. return output;
  79. }
  80. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
Running Shifting array...
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 2 in the list is : 0 0 1 1 1 1 1 1 1 
Array no. 3 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 4 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 5 in the list is : 0 0 1 0 0 4 0 0 1 
Array no. 6 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 7 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 8 in the list is : 0 0 1 1 1 1 1 1 1