Thursday, March 19, 2020
How Similes Work
How Similes Work A simile is a direct comparison of two different and often unrelated objects. Similesà are useful for making creative writing come to life. Common similes include run like the wind, busy as a bee, or as happy as a clam. Before looking at any examples, you should try a little brainstorming exercise. First, jot down a list of characteristics of the subject youre writing about. For example, is it noisy, dense, or annoying? Once you have a shortlist completed, look over those characteristics and try to imagine an unrelated object that shares those characteristics. This list of similes will help you come up with your own examples. Similes That Include the Word Like Many similes are easy to identify because they include the word like. The cat slipped through the crack like liquid.The delicious smell meandered through the house like a stream.That bed was like a pile of rocks.My heart is racing like a frightened rabbit.The fire alarm was like a screaming baby.Watching that movie was like watching paint dry.The winter air was like a cold razor.The hotel was like a castle.My brain was like a sun-baked brick during the exam.I shook like a rattlesnakes tail.Being grounded is like living in an empty desert.The alarm was like a doorbell in my head.My feet were like frozen turkeys.His breath was like a fog from a haunted bog. As-As Similes Some similes use the word as to compare two objects.à That kid can run as fast as a cheetah.Hes as cute as a frogs dimple.This sauce is as hot as the sun.My tongue is as dry as burnt toast.Your face is as red as hot coals.His feet were as big as a tree.The air was as cold as the inside of a freezer.These bed sheets are as scratchy as sandpaper.The sky is as dark as ink.I was as cold as a snowman.Im as hungry as a bear in springtime.That dog is as messy as a tornado.My sister is as shy as a newborn fawn.His words were as soft as snowflakes on a leaf. Similes can add a creative flourish to your paper, but they can be tricky to get right. And remember:à similes are great for creative essays, but not really appropriate for academic papers.
Tuesday, March 3, 2020
Using the ArrayList in Java
Using the ArrayList in Java Standard arrays in Java are fixed in the number of elements they can have. If you want to increase of decrease the elements in an array then you have to make a new array with the correct number of elements from the contents of the original array. An alternative is to use the ArrayList class. The ArrayList class provides the means to make dynamic arrays (i.e., their length can increase and decrease). Import Statement import java.util.ArrayList; Create an ArrayList An ArrayList can be created using the simple constructor: ArrayList dynamicArray new ArrayList(); This will create an ArrayList with an initial capacity for ten elements. If a larger (or smaller) ArrayList is required the initial capacity can be passed to the constructor. To make space for twenty elements: ArrayList dynamicArray new ArrayList(20); Populating the ArrayList Use the add method to append a value to the ArrayList: dynamicArray.add(10); dynamicArray.add(12); dynamicArray.add(20); Note: The ArrayList only stores objects so although the above lines appear to add int values to ArrayList the are automatically changed to Integer objects as they are appended to the ArrayList. A standard array can be used to populate an ArrayList by converted it to a List collection using the Arrays.asList method and adding it to the ArrayList using the addAll method: String[] names {Bob, George, Henry, Declan, Peter, Steven}; ArrayList dynamicStringArray new ArrayList(20); dynamicStringArray.addAll(Arrays.asList(names)); One thing to note about ArrayList is the elements dont have to be of the same object type. Even though the dynamicStringArray has been populated by String objects, it still can accept number values: dynamicStringArray.add(456); To minimize the chance of errors its best to specify the type of objects you want the ArrayList to contain. This can be done at the creation stage by using generics: ArrayList dynamicStringArray new ArrayList(20); Now the if we try to add an object that isnt a String a compile-time error will be produced. Displaying the Items in an ArrayList To display the items in an ArrayList the toString method can be used: System.out.println(Contents of the dynamicStringArray: dynamicStringArray.toString()); which results in: Contents of the dynamicStringArray: [Bob, George, Henry, Declan, Peter, Steven] Inserting an Item into the ArrayList An object can be inserted anywhere into the ArrayList index of elements by using the add method and passing the position for the insertion. To add the String Max to the dynamicStringArray at position 3: dynamicStringArray.add(3, Max); which results in (dont forget the index of an ArrayList starts at 0): [Bob, George, Henry, Max, Declan, Peter, Steven] Removing an Item from an ArrayList The remove method can be used to remove elements from the ArrayList. This can be done in two ways. The first is to supply the index position of the element to be removed: dynamicStringArray.remove(2); the String Henry in postion 2 has been removed: [Bob, George, Max, Declan, Peter, Steven] The second is to supply the object to be removed. This will remove the first instance of the object. To remove Max from the dynamicStringArray: dynamicStringArray.remove(Max); The String Max is no longer in the ArrayList: [Bob, George, Declan, Peter, Steven] Replacing an Item in an ArrayList Rather than removing an element and inserting a new one in its place the set method can be used to replace an element in one go. Just pass the index of the element to be replaced and the object to replace it with. To replace Peter with Paul: dynamicStringArray.set(3,Paul); which results in: [Bob, George, Declan, Paul, Steven] Other Useful Methods There are a number of useful methods to help navigate the contents of an arraylist: The number of elements contained within an ArrayList can be found using the size method: System.out.println(There are now dynamicStringArray.size() elements in the ArrayList);After all our manipulations of dynamicStringArray were down to 5 elements:There are now 5 elements in the ArrayList Use the indexOf method to find the index position of a particular element: System.out.println(The index position of George is : dynamicStringArray.indexOf(George));The String George is in index position 1:The index position of George is : 1 To clear all the elements from an ArrayList the clear method is used: dynamicStringArray.clear(); Sometimes it can be useful to see if the ArrayList has any elements at all. Use the isEmpty method: System.out.println(Is the dynamicStringArray empty? dynamicStringArray.isEmpty());which after clear method call above is now true:Is the dynamicStringArray empty? true
Subscribe to:
Posts (Atom)