Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- An algorithm is a series of steps you take to manipulate data.
- A data structure is the way data is arranged in memory.
- The main data structure operations (or algorithms) are:
- * Inserting
- * Deleting
- * Searching
- Here's a basic example of array structuring:
- ------------------------------BASIC EXAMPLE-----------------------------
- int[] arrayName = new int[3]; // this is saying to create 3 boxes for this array, 3 indexes and values
- int thirdValue = arrayName[2]; // this is calling for the 3rd variable, since we start by counting 0
- int[] arrayName = {12, 16, 24}; // we initialize and give value to each array index
- arrayName[1]; // returns 16 since we asked for the 2nd index in the array
- -----------------------------------------------------------------------------
- What about this one?
- -----------------------------------------------------------------------------
- String[][][] arrayName = {{{"000"}, {"100"}, {"200"}, {"300"}},
- {"010"}, {"110"}, {"210"}, {"310"}},
- {"020"}, {"120"}, {"220"}, {"320"}}};
- -----------------------------------------------------------------------------
- What's going on in this multidimensional array is basically here:
- arrayName[How Many Down (3)][How Many Across (4)][How Many of Those Groups (1)]
- So basically the first box represents how many down, so if I were to do arrayName[2][][] it would called "020".
- The second box represents how many across, and we have 4 indexes across.
- The third box represents how many of these groupings are we going to need. This one only has one group.
- With a linear search, what it does is look at every single indexed value in the entire array.
- -------------------------------------------------------------------------------------------------------------------------------
- public class ArrayStructures
- {
- private int[] theArray = new int[50];
- // storing the array size so we don't have to print the default values
- private int arraySize = 10;
- // we created this to just generate random numbers for the array
- public void generateRandomArray()
- {
- for (int i = 0; i < arraySize; i++)
- {
- theArray[i] = (int)(Math.random() * 10) + 10;
- }
- }
- // the print of the array arraySize
- public void printArray()
- {
- System.out.println("-----------");
- for (int i = 0; i < arraySize; i++)
- {
- System.out.print("I " + i + " I ");
- System.out.println(theArray[i] + " I");
- }
- System.out.println("-----------");
- }
- // we get the value at whatever index we ask of it
- public int getValueAtIndex(int index)
- {
- if(index < arraySize)
- {
- return theArray[index];
- }
- return 0;
- }
- // we look through the values to see if they contain the searchValue we ask of it
- public boolean doesArrayContainThisValue(int searchValue)
- {
- boolean valueInArray = false;
- for (int i = 0; i < arraySize; i++)
- {
- if(theArray[i] == searchValue)
- {
- valueInArray = true;
- }
- }
- return valueInArray;
- }
- // we delete the index we ask for it to delete
- public void deleteIndex(int index)
- {
- if (index < arraySize)
- {
- // we do arraySize - 1 because we have 1 less actual
- // value then our arraySize because of the 0 index
- for(int i = index; i < (arraySize - 1); i++)
- {
- // that is going to move up the variables
- theArray[i] = theArray[i + 1];
- }
- // since we no longer have enough spaces
- // we are going to decrement the arraySize
- arraySize--;
- }
- }
- // we insert the value we ask for it to insert
- public void insertValue(int value)
- {
- if (arraySize < 50)
- {
- theArray[arraySize] = value;
- arraySize++;
- }
- }
- // this will do a linear search to find the value we
- // ask for it to find us, and it'll give us the matches
- public String linearSearchForValue(int value)
- {
- boolean valueInArray = false;
- String indexesWithValue = "";
- System.out.print("The value was found in the following indexes: ");
- for(int i = 0; i < arraySize; i++)
- {
- if(theArray[i] == value)
- {
- valueInArray = true;
- System.out.print(i + " ");
- indexesWithValue += i + " ";
- }
- }
- if(!valueInArray)
- {
- indexesWithValue = "None";
- System.out.print(indexesWithValue);
- }
- System.out.println();
- return indexesWithValue;
- }
- public static void main(String args[])
- {
- ArrayStructures newArray = new ArrayStructures();
- newArray.generateRandomArray();
- newArray.printArray();
- System.out.println(newArray.getValueAtIndex(3));
- System.out.println(newArray.doesArrayContainThisValue(18));
- newArray.deleteIndex(4);
- newArray.printArray();
- newArray.insertValue(55);
- newArray.printArray();
- newArray.linearSearchForValue(17);
- }
- }
- /*
- -----------
- I 0 I 18 I
- I 1 I 11 I
- I 2 I 18 I
- I 3 I 11 I
- I 4 I 13 I
- I 5 I 17 I
- I 6 I 12 I
- I 7 I 14 I
- I 8 I 12 I
- I 9 I 12 I
- -----------
- 11
- true
- -----------
- I 0 I 18 I
- I 1 I 11 I
- I 2 I 18 I
- I 3 I 11 I
- I 4 I 17 I
- I 5 I 12 I
- I 6 I 14 I
- I 7 I 12 I
- I 8 I 12 I
- -----------
- -----------
- I 0 I 18 I
- I 1 I 11 I
- I 2 I 18 I
- I 3 I 11 I
- I 4 I 17 I
- I 5 I 12 I
- I 6 I 14 I
- I 7 I 12 I
- I 8 I 12 I
- I 9 I 55 I
- -----------
- The value was found in the following indexes: 4
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement