common arrayList methods

common arrayList methods

int size(): returns the number of elements in a array.

Boolean isEmpty():returns  true if list is empty falsi if it isn’t.

Boolean add(objectType  element) : returns true and adds element to the end of the list

Void add( int i, E element):insert element at ith position and shifts elements to fit it( increments the elements at i and after by 1.

ObjectType get(int i) : returns value if ith element

ObjectType set(int i, ObjectType element): replaces the ith element and returns the old value.

ObjectType remove (int i) removes ith element and returns its value.

Boolean contains (Object obj) returns true if the list has the object (equals method used for comparison).

Int indexOf(Object obj) returns index of the first occurence of obj in this arraylist, or -1 if it isn’t found.

String toString() returns a string representation of the list.

A link to the full list of refrences is below:

ArrayList methods (Oracle)

for each

for each

A for each loop(or enhanced for loop) is used to traverse a array, but they can’t change the values of elements with a primitive datatype because the variable given refers to a element that holds a copy of the element.

A for each loop is made like ;

for( data/objectTypeOfTheElementsInTheArray varName : ArrayName)

{

. . . //process varName

}

Also do not use a for each loop if you need to access the indices, because the example above is basically equal to:

for( int i =0; i<arrayName.length;i++)

{

data/objectTypeOfTheElementsInTheArray varName = arrayName[ i ];

. . . //process varName

}

A common use of for each loops include finding the arrays smallest or largest value.

two dimensional arrays

two dimensional arrays

A two dimensional array normally represents something along the lines of a box or a chart, or something else in a grid format.

THis kind of array has two [ ] after its name when you are trying to call its index, the first [ ] should have the rows index the second [ ] should have the column index. THe rows are horizontal and  the columns are vertical, the number of columns per row can also differ. Both of the indexes start at zero.

You declare a two dimensional array in two ways:

  • datatype[ ][ ]  arrayName = new datatype[numRows][numCols];
  •  datatype[ ][ ]  arrayName =

{   

{somevalueA, somevalueB, . . . } ,

{somevalueC, somevalueD , . . . }

} ;

You can get the number of rows by doing arrayName.length.

And you can get the number of columns by doing m[rowNumber].length.

one dimensional arrays

one dimensional arrays

One dimensional arrays are arrays that only have a single index involved, so they are a row or a list not a box, table or chart.

You can make a one dimensional array in several ways as shown below:

  • datatype[ ] arrayname= new datatype/object[sizeofarray];
  • datatype[ ] arrayname= {variableA,variableB, . . };

You can find the size or length of a array by doing arrayName.length, this is like  calling a public variable not a method so there are no parenthesis.

You add a element to an array by doing :

arrayName[index] = <what you want to add to the array as long as its type is the same as the arrays>;

Arrays

Arrays

Arrays are consecutive memory locations of the same data type under one name. Each memory location in a array is referred to as a element. The number of elements is the size or length of the array. The program often calls an element in a array by using the array name  followed by the index of the element in brackets which can be any variable or expression that results in a integer, but the index you call must be in the array or it will give you a IndexOutOfBoundsException.

The reason to use arrays is so you can easily access a group of related values, so for example a array would normally hold something like a list of the scores of basketball games during a year, it would be annoying to have to access each score individually, but with a array you can just use a for loop to get to all the scores.

An example of this is:

int sum= 0;

for(int i=0; k<sco.length; k++)

sum+=scores[k];

Instead of doing:

int sum = 0;

sum+= sco1;

sum+=sco2;

. . .

Which also has some problems associated with it because can’t be certain how many scores there are, which means it would be hard to make the method general instead of specific without using a array.

Some things to keep in mind when it comes to arrays are, that if the elements are of a class type then the array contains references to the objects of that type not the object itself, this means that when a array is made its initial values are set to null, while numeric datatypes would have it set to zero and Boolean would have it set to false. Also you cant change a arrays size once it has been declared and initialized, either of the ways shown above. So to make a new one you need to create a entirely new array and copy the values from the old array to the new one. Also, arrays are always passed to methods or constructors as references, so it will be able to change the original array.

**the index starts at zero so the first element in a array will be at index zero NOT one, but when getting the length or size of a array it will start at 1, so length will return the last index +1.**

Array has several different sub-parts, go to the links below to learn about those:

for each loops

One dimensional arrays

two dimensional arrays

You can make three + dimension arrays but they aren’t used very often and I personally have never had to deal with them, so a post on that won’t happen for a long time.

Other similar topics are :

Arraylists

codeHs user input

codeHs user input

CodeHS user input uses a one line format.

THat format is:

datatype varname = readDatatype(“prompt”);

This may diifer if you are making a string variable in which case instead of readdatatype it is readLine.

An example of user input being used is

int l = readInt(“Eneter a positive interger”);

This information is also shown/explained in the docs of codeHS when you are making a program.

Eclipse user input

Eclipse user input

Eclipse does user input according to the given java packages on the oracle website, which is where you download eclipse.The specific java package is java.util.Scanner.  So, the first thing you need to do is import the package if the version of eclipse doesn’t import it automatically.

Which is done by doing:

import java.util.Scanner;

In the space before the class header along with any other imports.

The second thing you need to do is make a scanner object

Which is typically done in the public method and is done by a piece of code along the lines of:

Scanner varname= new Scanner(System.in);

The next step is to typically make the prompt. Which is normally done with:

System.out.print(“prompt”);

The final step is to turn the answer to the prompt into user input so doing something along the lines of:

int k = varname.nextInt();

So all together it is something along the lines of:

import java.util.Scanner;

public class Name

{

public static void main(String[] args)

{

Scanner varname= new Scanner(System.in);

System.out.print(“prompt”);

int k = varname.nextInt();

varname.close();

}

}

varname.close (); is just closing the scanner so you can’t put in anymore input, but I found it isn’t normally completely necessary.

The scanner class can also be used from what I can tell, since I have never used it this way personally, to get things from files on your computer or to take things out of a string by using the delimiter part of the class.

A link to the scanner package is below:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

do-while

do-while

The do-while loops is similar to the while loop except it will always go through the code in the loop at least once no matter what. Like the while loops it has three parts, the initialization of the variable in the condition, the testing of the variable(condition) and the change in the variable.

THe syntax of the do-while loop is:

do{

}while(condition);

The do-while loop isn’t as common as the other loops since it is similar to the while loop and its normally pretty simple to edit the condition for a while loop allow for the loop to always be gone through at least once.

Comments

Comments

THere are two main kinds of comments single line and multi line,

An example of single line is:

//single line comment

A single line comment is often used to explain obscure code, so code where it isnt necessarily intuitive what it does.

A example of multi line is:

/**

*multi line comment

*

*

*/

Another form is:

/*

*

*/

The former example of multi-line comment are used for api documentation using the javadoc tool. While the latter is just a way to make a comment multiple lines and should be avoided as much as possible since it makes the code more clunky and generally the better pieces of programming  can easily be understood as long as you do api documentation.

The format for api documentation is basically

/**

*<p>explanation of method</p>

*@param parameter

*@param parameter

*@return what the return is suppose to be

*/

Although it differs depending on what it is for example normally there is a comment before the beginning of a class explaining the class’s purpose and who the author is.

The video on comments is below