Advertisement
AL4ST4I2

ex1-2 24 giugno

Jul 15th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package esercizio3.iterable;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5.  
  6. public class IterableString {
  7.  
  8. public String testo;
  9.  
  10. public IterableString(String s)
  11. {
  12. this.testo = s;
  13. }
  14.  
  15. public Iterator<Character> iterator()
  16. {
  17. return new MYit();
  18. }
  19.  
  20. private class MYit implements Iterator<Character>
  21. {
  22. int pos = 0;
  23. @Override
  24. public boolean hasNext() {
  25. return pos >= 0 || pos < IterableString.this.testo.length();
  26. }
  27.  
  28. @Override
  29. public void remove() {
  30.  
  31. if (!hasNext()) throw new IllegalStateException("pos minore");
  32.  
  33. Character toBeRemoved = IterableString.this.testo.charAt(pos);
  34.  
  35. String temp = new String();
  36. for (int i = 0; i < IterableString.this.testo.length(); i++)
  37. {
  38. if (pos != i)
  39. temp = temp + IterableString.this.testo.charAt(i);
  40. }
  41.  
  42. IterableString.this.testo = temp;
  43.  
  44. }
  45.  
  46. @Override
  47. public Character next() {
  48.  
  49. if(!hasNext())
  50. {
  51. throw new NoSuchElementException("Has no next element");
  52. }
  53. Character ritorno = IterableString.this.testo.charAt(pos);
  54. pos++;
  55.  
  56. return ritorno;
  57. }
  58. }
  59. }
  60.  
  61.  
  62. ---------------------------------------------------------------------------------------------------------------------------------------
  63.  
  64. package esercizio1;
  65.  
  66. import java.util.Arrays;
  67.  
  68. public class Esercizio1 {
  69.  
  70. public void ordina(int[][] m)
  71. {
  72.  
  73. if ( m == null || m.length == 0) throw new IllegalArgumentException(new String("fuck you"));
  74. int[] buffer = new int[m.length * m[0].length];
  75. int buffIter = -1;
  76.  
  77. for (int i = 0; i < m.length; i++)
  78. {
  79. for (int j = 0; j < m[i].length; j++)
  80. {
  81. buffer[++buffIter] = m[i][j];
  82. }
  83. }
  84. Arrays.sort(buffer);
  85. System.out.println(Arrays.toString(buffer));
  86. buffIter = 0;
  87. for (int i = 0; i < m.length; i++) {
  88. for (int j = 0; j < m[0].length; j++) {
  89. m[i][j] = buffer[buffIter++];
  90.  
  91. }
  92. }
  93.  
  94. System.out.println(Arrays.deepToString(m));
  95. }
  96.  
  97.  
  98.  
  99.  
  100. public static void main(String args[])
  101. {
  102. Esercizio1 es = new Esercizio1();
  103. //int[][] in = {{2,54,6},{3,67,24},{43,68,43},{90,74,76}};
  104. int[][] in = null;
  105. es.ordina(in);
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement