Przejdź do głównej zawartości

Java -Arrays

Array - data structure that allows us to store multiple values of the same type into a single variable

Default value - zero
Arrays are zero indexed - Array with ten elements will be indexed from 0 to 9
Array[]

How to create array:

  1. int[] array = new int[5]; (boxes have value zero - secured slot in memory to hold values)
  2. int[] myArray = {1,2,3,4,5}; this type of initializing an array is also known as an anonymous array

boolean type arrays - default value is false

Errors while working with arrays


  1. ArrayIndexOutOfBoundsExeption

  2. int[] my Array = {10,35,45,78,55}
    myArray[5] = 55; out of bound
    index 5 does not exist





  1. int[] myArray = {10,15,20,17,18};


  2. for (int i=1; i < myArray.lenght; i++) 

    System.out.println("value= " + myArray[i]

    it will return values from index 1

How to print out simple int array?
example:

int[] myArray = new int[2];myArray[0]=0;myArray[1]=1;


System.out.println("My array: " + 
        Arrays.toString(myArray));


static StringtoString(int[] a)
Returns a string representation of the contents of the specified array.


public class Arrays - This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

Komentarze

Popularne posty z tego bloga

Stored Procedures - JDBC Java SQL

Stored Procedures: group of SQL statements that perform a particular task  Normally created by DBA Can have any combination of input, output, and input/output parameters Benefits: Performance  - compiled once but executable more times Productivity and Ease of Use  - avoid redundant code, extend SQL Database functionality  Scalability   Maintainability Interoperability Replication Security To call stored procedure from Java The JDBC API provides the CallableStatement CallableStatement myCall = myConn.prepareCall("{ call some_stored_procedure() }"); ... myCall.execute(); JDBC API parameter types IN default INOUT OUT Stored Procedure can return result sets EXAMPLE: Created stored procedure on MySQL side - NAME:  increase_salaries_for_department DELIMITER $$ DROP PROCEDURE IF EXISTS ` increase_salaries_for_department `$$ CREATE DEFINER=`student`@`localhost` PROCEDURE `increase_salaries_for_departme...

Java - variables , data types

Data Types Data types in Java are classified in to types: Primitive - which include Integer, Character, Boolean and Floating Point Non-primitive - which includes Classes, Interfaces and Arrays Integer : byte 1 byte short 2 bytes int 4 bytes long 8 bytes Floating Point : number + fractional parts  float double Character : stores character Boolean : hold true false value Variables  Instance Variables (non-Static Fields) -  Objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Class Variables - Static Fields - any field with static modifier says that there is only one copy of this variable in existence static int myClassVariable = 6;  Local Variables - a method stores it's temporary state of local variables int count=0; Parameters - variables which will be passed to the methods of a class

Java Naming Convention

Java Naming Convention Naming Convention Oracle documentation: Thinks to name in Java are: Classes CamelCase Class names should be nouns - they represent things  Start with capital letter e.g. MyNewClass ArrayList LinkedList String TopSong Main Variables mixedCase start with lower case Meaningful and indicative  Packages low case unique internet domain name - reversed as a prefix Examples: Invalid Switch.supplier.com  Valid com.supplier._switch Methods mixedCase Often verbs Reflect the function size() getName() addPlayer() Constants represent constants values ALL_UPPER_CASE MAX_INT SEVERITY_ERROR P1 = = 3,141592653 Type Parameters single character, capital letters E-Element T-Type V-Value Interfaces