Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. int[][] array = new int[2][3];
  2. array[0][0] = 1;
  3. array[0][1] = 2;
  4. array[1][0] = 3;
  5. array[1][1] = 4;
  6. array[0][2] = 5;
  7. array[1][2] = 6;
  8.  
  9. public class Application {
  10.  
  11. public static void main(String[] args) {
  12. int[][] array = new int[2][3];
  13. array[0][0] = 1;
  14. array[0][1] = 2;
  15. array[1][0] = 3;
  16. array[1][1] = 4;
  17. array[0][2] = 5;
  18. array[1][2] = 6;
  19. int[] newArray = new int[array.length];
  20. for (int i = 0; i < array.length; i++) {
  21. for (int j = 0; j < array[i].length; j++) {
  22. newArray[i] += array[i][j];
  23. }
  24. }
  25. Arrays.stream(newArray).forEach(System.out::println);
  26. }
  27.  
  28. }
  29.  
  30. package array;
  31.  
  32. /**
  33. *
  34. * @author vvm
  35. */
  36. public class ArrayCount {
  37.  
  38. public static void main(String[] args) {
  39. int[][] array = new int[2][3];
  40. for (int i = 0; i < 2; i++) {
  41.  
  42. for (int j = 0; j < 3; j++) {
  43. array[i][j] = i+1;
  44.  
  45. }
  46. }
  47. for (int i = 0; i < 2; i++) {
  48. System.out.println("");
  49. for (int j = 0; j < 3; j++) {
  50. System.out.print(" array[" + i + "]" + "[" + j + "]" + " = " + array[i][j]);
  51. }
  52. }
  53. }
  54. }
  55.  
  56. int rowsCount = array[0].length;
  57. for(int row = 0; row < rowsCount; row++)
  58. {
  59. int sum = 0;
  60. for(int i = 0; i < array.length; i++)
  61. {
  62. int cell = array[i][row];
  63. sum += cell;
  64. // debug (remove next line if not needed!)
  65. System.out.print(cell + (i < array.length - 1 ? " + " : " = "));
  66. }
  67.  
  68. System.out.println(sum);
  69. }
Add Comment
Please, Sign In to add comment