Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet91/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   87   88   89   90   91   92   93   94   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Working with Objects
JavaScript allows user access to a number of existing objects. One of these objects is an
array. This section discusses all the basics related to this chapter. Dealing with objects in
JavaScript also includes creation and handling of customized objects. However, this topic
shall be covered in the chapter on JavaScript and jQuery.
Arrays
A collection of similar objects that are sequenced contiguously are referred to as an array.
This  array  is  given  a  name  and  each  element  can  be  accessed  using  the  indexer,  in  the
following form:
Let arrName[] be an array of names. The element arrName[2] refers to the third element
of the array.
An array can be created using the following three methods:
Insertion of Items Using Indexer
An array can be created using the new keyword and then, elements can be added
into the array by assigning values to independent elements of the array. The new
keyword creates an instance of the object Array using the constructor for the same.
Sample implementation:
var arrName = new Array();


arrName [0] = ‘Jack’;
arrName [1] = ‘Alex’;
Condensed Array
The second type of implementation also uses the new keyword. However, in this
case, the values are assigned to the elements as arguments to the constructor of the
Array object.
Sample implementation:
var arrName = new Array(‘Jack, ‘Alex’);
Literal Array
In this type of array definition, values are provided within the square brackets.
Sample implementation:
var arrName = [ ‘Jack, ‘Alex’];
The advantage of using the first type of definition is that it allows you to assign values to
the  elements  anywhere  in  the  code.  On  the  other  hand,  the  second  and  third  type  of
implementation requires you to have the exact list of elements with you beforehand.
There  are  some  properties  associated  with  all  object.  The  one  property  that  can  come  in
handy to you is length, which is a read-only value and when called return the number of
elements present in the array. You can use this property in loops and conditions.
Objects  can  also  have  their  own  functions.  These  functions  are  called  methods.  The
methods available for Array include:
concat
Returns an array, which is the concatenation of the two arrays supplied to it.
indexOf


Finds the location of the element concerned in the array and returns the index of
the same.
join
This  method  concatenates  all  the  values  present  in  the  array.  However,  all  these
values are separated by a comma by default. You can specify a delimiter of your
choice as well.
lastIndexOf
This method works similarly as indexOf. However, it performs the search from the
last  element  of  the  array.  Therefore,  it  returns  the  index  of  the  last  element  that
matches the specified criterion.
pop
Removes the last element and returns its value.
push
Adds the concerned element to the end of the array and returns the changed value
of length.
reverse
This  method  reverses  the  order  of  the  array  elements.  The  original  array  is
modified by this method.
shift
Removes  and  returns  the  first  value.  If  the  array  is  empty,  then  undefined  is
returned.
slice
This  method  requires  two  arguments,  start  index  and  end  index.  A  new  array  is


created  with  elements  same  as  the  elements  present  at  indexes  (start  index)  and
(end index – 1).
sort
This method sorts the elements and modifies the original array.
splice
This  method  removes  and  adds  elements  to  the  specified  array.  The  arguments
taken  by  this  method  are  start  index  (index  from  where  the  system  should  start
removing elements), number of elements to be removed and elements to be added.
If the value passed for number of elements is 0, then no elements are deleted. On
the other hand, if this value is greater than the size of the array, all elements from
the start index to the end of the array are deleted.
toString
This  method  creates  a  string,  which  is  a  comma  separated  concatenated  string  of
all the elements present in the array.
unshift
This  method  adds  an  element  at  the  first  location  of  the  array  and  return  the
modified value of length.
valueOf
This  method  returns  a  string,  which  is  the  concatenated,  comma-separated  string
containing all the values present in the array.
Note:
1.  When working with functions, you can pass the whole array (using the array name)
or a particular element of the array (using array name[indexer]).


2.  Array  elements  can  be  modified  by  accessing  the  element  using  the  indexer.  For
example, arrName[1] = ‘Danny’; assigns the value ‘Danny’ to the second element
of the array.

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   87   88   89   90   91   92   93   94   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling