In Java, some of the most common
task is to create, store, and manipulate information of a data structure. I
learned how to use classes and methods built into Java to write these programs.
To start off, manipulating a data is very common and Java provides control
statements to make a task run repeatedly. The controls statement I learned is
called a for loop. Just like a loop, a
for loop runs a code repeatedly until
the condition is met. In the book it says, "the for loop is the workhorse of the repetitive loop structure because it's easy to create, easy to debug, and easy to understand" (Mueller 187). Here is an example of the code.
In this line of code, it shows that
the initial number starts at 0. Then I wrote a Boolean statement and so if the initial
number is less than ten, then it will evaluate the expression as true which
will result in the number to be added by one. Since this is a for loop, the code will repeat again and
again until the statement is false. For the statement to be false the number
would have to be greater than ten. So, here is what the output will look like.
The next thing I learned was ArrayList. An ArrayList is used to store a list of data. The ArrayList is a pre-defined Java class so it is written in a certain
way. An ArrayList is written out like
this: “ArrayList<Integer>” followed by any object that is created. With ArrayList, it can be used to store data
such as integers and to do this, you would write out the object name followed
by “. add” and then put the number inside the parenthesis. I created an ArrayList with the object name of “batteryPercentage”.
I then added a list of number and it looked like this.
Now that I have a list of numbers
of my “batteryPercentage” stored in the ArrayList,
I can use an element’s index to access them anytime. An element’s index is the
location of the numbers in the ArrayList.
So, the first element in my ArrayList
is the battery Percentage of 100 and it is at the position of 0. If I wanted to
access the batteryPercentage with the value of 95, I would write out the code
like this.
Lastly, to add another value to the
array list it is slight change to the “add” method. To insert another value, it
would be written just like the “add” method we’ve seen in the previous pictures,
but you would write out the position of where you would want the new value to
be placed into the list before putting the number in. So, if I wanted the “batteryPercentage”
with the value of 50 to be placed in the ArrayList
at position 2, the code would look like this.
The position of the other value will shift one up or one down depending on where the new value is placed |
To sum it up, for loops help manipulate the entire data structures and ArrayList is used to store specific data. For loops are helpful to program computers to perform a certain task repeatedly and ArrayList can be used to help organize the data structure and makes it easier to access certain values. How do you think for loops or ArrayList can be helpful when writing a specific program?
Mueller, John Paul. Java eLearning Kit For Dummies. Wiley, 2014.
No comments:
Post a Comment