Advertisement
Crenox

Derek Banas Array Structures Java Program

Jul 29th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. An algorithm is a series of steps you take to manipulate data.
  2. A data structure is the way data is arranged in memory.
  3.  
  4. The main data structure operations (or algorithms) are:
  5.  
  6. * Inserting
  7. * Deleting
  8. * Searching
  9.  
  10. Here's a basic example of array structuring:
  11. ------------------------------BASIC EXAMPLE-----------------------------
  12. int[] arrayName = new int[3]; // this is saying to create 3 boxes for this array, 3 indexes and values
  13. int thirdValue = arrayName[2]; // this is calling for the 3rd variable, since we start by counting 0
  14. int[] arrayName = {12, 16, 24}; // we initialize and give value to each array index
  15. arrayName[1]; // returns 16 since we asked for the 2nd index in the array
  16. -----------------------------------------------------------------------------
  17.  
  18. What about this one?
  19. -----------------------------------------------------------------------------
  20. String[][][] arrayName = {{{"000"}, {"100"}, {"200"}, {"300"}},
  21. {"010"}, {"110"}, {"210"}, {"310"}},
  22. {"020"}, {"120"}, {"220"}, {"320"}}};
  23. -----------------------------------------------------------------------------
  24. What's going on in this multidimensional array is basically here:
  25. arrayName[How Many Down (3)][How Many Across (4)][How Many of Those Groups (1)]
  26.  
  27. So basically the first box represents how many down, so if I were to do arrayName[2][][] it would called "020".
  28.  
  29. The second box represents how many across, and we have 4 indexes across.
  30.  
  31. The third box represents how many of these groupings are we going to need. This one only has one group.
  32.  
  33. With a linear search, what it does is look at every single indexed value in the entire array.
  34.  
  35. -------------------------------------------------------------------------------------------------------------------------------
  36. public class ArrayStructures
  37. {
  38. private int[] theArray = new int[50];
  39.  
  40. // storing the array size so we don't have to print the default values
  41. private int arraySize = 10;
  42.  
  43. // we created this to just generate random numbers for the array
  44. public void generateRandomArray()
  45. {
  46. for (int i = 0; i < arraySize; i++)
  47. {
  48. theArray[i] = (int)(Math.random() * 10) + 10;
  49. }
  50. }
  51.  
  52. // the print of the array arraySize
  53. public void printArray()
  54. {
  55. System.out.println("-----------");
  56.  
  57. for (int i = 0; i < arraySize; i++)
  58. {
  59. System.out.print("I " + i + " I ");
  60. System.out.println(theArray[i] + " I");
  61. }
  62.  
  63. System.out.println("-----------");
  64. }
  65.  
  66. // we get the value at whatever index we ask of it
  67. public int getValueAtIndex(int index)
  68. {
  69. if(index < arraySize)
  70. {
  71. return theArray[index];
  72. }
  73.  
  74. return 0;
  75. }
  76.  
  77. // we look through the values to see if they contain the searchValue we ask of it
  78. public boolean doesArrayContainThisValue(int searchValue)
  79. {
  80. boolean valueInArray = false;
  81.  
  82. for (int i = 0; i < arraySize; i++)
  83. {
  84. if(theArray[i] == searchValue)
  85. {
  86. valueInArray = true;
  87. }
  88. }
  89.  
  90. return valueInArray;
  91. }
  92.  
  93. // we delete the index we ask for it to delete
  94. public void deleteIndex(int index)
  95. {
  96. if (index < arraySize)
  97. {
  98. // we do arraySize - 1 because we have 1 less actual
  99. // value then our arraySize because of the 0 index
  100. for(int i = index; i < (arraySize - 1); i++)
  101. {
  102. // that is going to move up the variables
  103. theArray[i] = theArray[i + 1];
  104. }
  105.  
  106. // since we no longer have enough spaces
  107. // we are going to decrement the arraySize
  108. arraySize--;
  109. }
  110. }
  111.  
  112. // we insert the value we ask for it to insert
  113. public void insertValue(int value)
  114. {
  115. if (arraySize < 50)
  116. {
  117. theArray[arraySize] = value;
  118.  
  119. arraySize++;
  120. }
  121. }
  122.  
  123. // this will do a linear search to find the value we
  124. // ask for it to find us, and it'll give us the matches
  125. public String linearSearchForValue(int value)
  126. {
  127. boolean valueInArray = false;
  128.  
  129. String indexesWithValue = "";
  130.  
  131. System.out.print("The value was found in the following indexes: ");
  132.  
  133. for(int i = 0; i < arraySize; i++)
  134. {
  135. if(theArray[i] == value)
  136. {
  137. valueInArray = true;
  138.  
  139. System.out.print(i + " ");
  140.  
  141. indexesWithValue += i + " ";
  142. }
  143. }
  144.  
  145. if(!valueInArray)
  146. {
  147. indexesWithValue = "None";
  148.  
  149. System.out.print(indexesWithValue);
  150. }
  151.  
  152. System.out.println();
  153.  
  154. return indexesWithValue;
  155. }
  156.  
  157. public static void main(String args[])
  158. {
  159. ArrayStructures newArray = new ArrayStructures();
  160.  
  161. newArray.generateRandomArray();
  162.  
  163. newArray.printArray();
  164.  
  165. System.out.println(newArray.getValueAtIndex(3));
  166.  
  167. System.out.println(newArray.doesArrayContainThisValue(18));
  168.  
  169. newArray.deleteIndex(4);
  170.  
  171. newArray.printArray();
  172.  
  173. newArray.insertValue(55);
  174.  
  175. newArray.printArray();
  176.  
  177. newArray.linearSearchForValue(17);
  178. }
  179. }
  180. /*
  181. -----------
  182. I 0 I 18 I
  183. I 1 I 11 I
  184. I 2 I 18 I
  185. I 3 I 11 I
  186. I 4 I 13 I
  187. I 5 I 17 I
  188. I 6 I 12 I
  189. I 7 I 14 I
  190. I 8 I 12 I
  191. I 9 I 12 I
  192. -----------
  193. 11
  194. true
  195. -----------
  196. I 0 I 18 I
  197. I 1 I 11 I
  198. I 2 I 18 I
  199. I 3 I 11 I
  200. I 4 I 17 I
  201. I 5 I 12 I
  202. I 6 I 14 I
  203. I 7 I 12 I
  204. I 8 I 12 I
  205. -----------
  206. -----------
  207. I 0 I 18 I
  208. I 1 I 11 I
  209. I 2 I 18 I
  210. I 3 I 11 I
  211. I 4 I 17 I
  212. I 5 I 12 I
  213. I 6 I 14 I
  214. I 7 I 12 I
  215. I 8 I 12 I
  216. I 9 I 55 I
  217. -----------
  218. The value was found in the following indexes: 4
  219. Press any key to continue . . .
  220. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement