• No se han encontrado resultados

2. Crecimiento de Madrid

2.1 Población y límites de la Villa

Multi dimension arrays in java are represented as array of arrays.

Syntax for declaring & creating a 2-D array:

datatype arrayname[][] = new datatype[size1][size2];

(or) datatype arrayname[][];

arrayname = new datatype[size1][size2];

Rules:

* At the time of array declaration we can specify the pair of square braces either before array name (or) after the array name.

int arr1[][];

int []arr2[];

int [][]arr3; //All are valid.

int[] arr4[];

` int[][] arr5;

int[][] arr6;

* At the time of array declaration, we must not specify the size of the array.

* At the time of array creation, specifying the first dimension is mandatory & the remaining dimensions are optional.

int [][]arr = new int[3][4];

(or) int [][]arr;

arr = new int[3][4];

* In multi dimensional array, the top level arrays will contain address of bottom level arrays.

* In a multi dimensional array, only the last level will contain values & remaining all top levels will contain addresses.

* The bottom level arrays can contain either same number of elements (or) different number of elements.

int arr[][] = new int[3][4];

(or)

int[][] arr = new int[3][];

arr[0] = new int[4];

arr[1] = new int[4];

arr[2] = new int[4];

int[][] arr = new int[3][];

arr[0] = new int[3];

arr[1] = new int[2];

arr[2] = new int[4];

int [][][] arr;

arr = new int[3][][];

arr[0] = new int[2][];

arr[0][0] = new int[2];

arr[0][1] = new int[3];

arr[1] = new int[3][2];

arr[2] = new int[2][1];

Declaring Creating & Initializing the array elements in a single line:

int [][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

double [][] arr = {{1.1,2.2,3.3},{4.4,5.5,6.6,7.7}};

Ex:

Class Test{

Psum (String[] args){

int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};

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

for(int j=0;j<arr[i].length;j++){

System.out.println(arr[i][j]+"");

}

System.out.println();

}

for(int[] x:arr){

for(int y:x){

System.out.println(y+"");

}

System.out.println();

} } }

Command Line Arguments:

The values that we pass into the program at the time of execution(runtime) are called as command line arguments. The command line arguments can be used for passing the values dynamically into the program & they can be used for avoiding “hardcoding”.

Hardcoding is the process of passing values to the variables with in the program. It is recommended not to use hardcoding in a program.

class Addition {

 public static void main(String[] abc){

int a = Integer.parseInt(abc[1]);

int b = Integer.parseInt(abc[1]);

System.out.println(a+b);

} }

 Java Addition.java

Java Addition 11 22 o/p: 33

Java Addition 33 44 o/p: 77

Java Addition 55 66 o/p: 121

* The command line arguments will be passed at the time of execution, at the command prompt

& after the class name.

* We can pass any no of values & any type of values separated by a space.

* The command line arguments that we pass will be stored into the string array of the main method.

* As a programmer, we are supposed to declaring the array i.e., we specify the array type & array name.

* The creation of the array is done by JVM & array size will be decided by JVM based on the no of arguments passed. Once the array is created, the arguments will be assigned into the array elements.

String abc[]; //Done by the programmer in main method abc = new String[2];

abc[0] = “11”;

abc[1] = “22”;

* If we do not pass any arguments then JVM will create an array of size “0”.

abc = new String[0]; //Where we can store ‘no’ values

* The creation of the array & the assignments of the values will be performed by the JVM just  before calling the main method.

* We can pass any type of values as command line arguments which will be stored in string format. If we want to use those values in our program then we can convert the values from string type to all primitive types except character, which is called as “parsing”.

Ex:

Integer.parseInt() Float.parseFloat() Double.parseDouble() Long.parseLong()

Boolean.parseBoolean() Short.parseShort()

Byte.parseByte()

Why should the main method take string array as the parameter?

If we pass int array to the main method then we can pass only integers, if we pass double array to the main method, then we can pass only double, floating point values, if we pass boolean array then we can pass boolean values but if we pass string array then we can pass integers, floating points, boolean, character values. We can convert all values except character.

Program to read data from keyword:

import java.io.*;

class Addition {

 public static void main(String abc[])throws IOException {

BufferReader br = new BufferReader(new InputStreamReader(System.in));

System.out.println("Enter First Number");

string fno = br.readline();

int

int a a = = Integer.parseInt(fno);Integer.parseInt(fno);

System.out.println("Enter Second Number");

System.out.println("Enter Second Number");

string sno = br.readline();

string sno = br.readline();

int b =

int b = Integer.parseInt(sno);Integer.parseInt(sno);

System.out.println("sum:"+(a+b));

System.out.println("sum:"+(a+b));

} } }

}

String String

* The string is a predefined used

* The string is a predefined used for storing group of characters.for storing group of characters.

* The string class objects can be created in two ways.

* The string class objects can be created in two ways.

i. by using new operator i. by using new operator

String str = new String(“hello”);

String str = new String(“hello”);

ii. by enclosing group of characters directly within double

ii. by enclosing group of characters directly within double codes.codes.

String str = “hello”;

String str = “hello”;

* The string objects created by both the mechanisms are immutable objects. Once the string

* The string objects created by both the mechanisms are immutable objects. Once the string object is created which we cannot modify the content of the object, but the reference of the string object is created which we cannot modify the content of the object, but the reference of the string object can be modified.

object can be modified.

String s1 = new String(“hai”);

String s1 = new String(“hai”);

s1 = new String(“bye”);

s1 = new String(“bye”);

Difference between two mechanisms of creating string object?Difference between two mechanisms of creating string object?

1. String object created by new operator 1. String object created by new operator

i. location:

i. location: The string object are created by using new operator then they are used in newThe string object are created by using new operator then they are used in new memory.

memory.

ii. Allocation:

ii. Allocation: If we are storing string objects in heap memory then it will never verifyIf we are storing string objects in heap memory then it will never verify whether the heap contains objects with same content or not, it will always create a new object.

whether the heap contains objects with same content or not, it will always create a new object.

String s1 = new String(“hai”);

String s1 = new String(“hai”);

s1 = new String(“bye”);

s1 = new String(“bye”);

String s2 = new String(“bye”);

String s2 = new String(“bye”);

Unreferenced Object:

Unreferenced Object:

If object does not have any live reference i.e., if an object is not pointed or If object does not have any live reference i.e., if an object is not pointed or referenced by any vtariable then that object will be called as Unreferenced Object.

referenced by any vtariable then that object will be called as Unreferenced Object.

iii. Deallocation:

iii. Deallocation:  If the heap memory contains unreferenced object then it will contain  If the heap memory contains unreferenced object then it will contain garbage collector.

garbage collector.

2. String object created by placing directly in “ ”:

2. String object created by placing directly in “ ”:

i. location:

i. location: If the string content is placed directly within “ ” then they are stored in stringIf the string content is placed directly within “ ” then they are stored in string content pool.

content pool.

ii. Allocation:

ii. Allocation: If string object are placed in string content pool then it will always verify If string object are placed in string content pool then it will always verify whether the string constant pool contains objects with same content or not. If available then refer whether the string constant pool contains objects with same content or not. If available then refer to the existing object otherwise create a new object.

to the existing object otherwise create a new object.

String s1 = “hai”;

String s1 = “hai”;

s2 = “bye”;

s2 = “bye”;

String s2 = “bye”;

String s2 = “bye”;

iii. Deallocation: If string constant pool contains unreferenced object then it will be deallocated by string constant pool when it is completely filled.

* If a string object is created by using new operator then the string object will be stored in both heap memory location & string memory poll.

* But the reference will point to the object available in heap memory.

String str = new String(“hello”);

* If we create a string object b y enclosing group of characters directly within “ ” then we store only in string constant pool.

Methods of String Class:

1. int length():

This method will return the count of no of char available in a string.

Ex:

String str = new String(“java program”);

System.out.println(str);

System.out.println(str.length());

2. char charAt(int index):

This method will return a character available at specified index position.

Note: The index of a string begins from zero.

Ex:

String str = new String(“java program”);

System.out.println(str.charAt(1));

System.out.println(str.charAt(12));

3. String Concat(string):

This method can be used to append the contents of one string to another string.

Ex:

String s1 = new String(“java”);

String s2 = new String(“program”);

System.out.println(s1.concat(s2));

System.out.println(s1);

System.out.println(s2);

O/P: java program  java

 program

4. int CompareTo(String):

This method can be used for comparing the unicode values of the characters available in the string. This method will do the comparision by considering their case.

S1 < S2 -ve S1 > S2 +ve S1 == S2 0

5. int CompareToIgnoreCase(String):

This method can be used for comparing the unicode values of the characters available in the string. This method will compare by ignoring their case.

Ex:

String s1 = new String(“A”);

String s2 = new String(“a”);

System.out.println(s1.compare(s2));

System.out.println(s1.compareToIgnoreCase(s2));

O/P: -32 0

6. boolean equals(String):

This method can be used for comparing contents of the strings by considering their case.

7. boolean equalsIgnoreCase(String):

This method can be used for comparing contents of the strings by ignoring their case.

Ex:

String s1 = new String(“ABCD”);

String s2 = new String(“abcd”);

System.out.println(s1.eqals(s2));

System.out.println(s1.equalsIgnoreCase(s2));

O/P: false True

8. boolean startswith(String):

This method can be used to check whether a string begins with specified string or not.

9. boolean endswith(String):

This method can be used to check whether a string ends with specified string or not.

Note: start with method & ends with methods are case sensitive.

Ex:

String s = new String(“java program”);

System.out.println(s.startswith("jav"));

System.out.println(s.startswith("JAV"));

System.out.println(s.endswith("ram"));

O/P:  true false true

10. int indexOf(char):

This method will return the index of the first occurrence of the specified character.

11. int lastindexOf(char):

This method will return the index of the last occurrence of the specified character.

Note: The index of method & last index of method will return -1, if specified character is not available in the string.

Ex:

String s = new String(“java program”);

System.out.println(s.indexOf('a'));

System.out.println(s.indexOf('p'));

System.out.println(s.indexOf('t'));

System.out.println(s.lastindexOf('a'));

System.out.println(s.lastindexOf('p'));

System.out.println(s.lastindexOf('z'));

O/P:1 5 -1 10 5 -1

12. string replace(char old, char new):

This method can be used for replacing all the occurrence of specified character with new character.

Ex:

String s = new String(“java jug jungle”);

System.out.println(s.replace('j','b'));

O/P: bava bug bungle

13. string substring(int index):

This method will retrieve a part of string beginning from specified ind ex position upto the end of the string.

14. string substring(int index, int offset):

This method can be used to retrieve a part of string beginning from specified index position upto the specified offset.

Note: The offset of a string represents a position of a character in a string.

* The offset starts from 1.

Ex:

String s = new String(“java program”);

System.out.println(s.substring(3));

System.out.println(s.substring(2,9));

O/P: va program ava prog

15. string toLowerCase():

This method can be used to converting contents of string completely to lower case.

16. string toUpperCase():

This method can be used to converting contents of string completely to upper case.

Ex:

String s = new String(“java program”);

System.out.println(s);

System.out.println(s.toLowerCase());

System.out.println(s.toUpperCase());

O/P:  java program  java program

JAVA PROGRAM

17. string trim(): leading trailing

This method can be used to remove the leading trailing space available in a string.

Ex:

String s = new String(“java program”);

System.out.println(s+"bye");

System.out.println(s.trim()+"bye");

O/P: java program bye  java programbye

18. string inter():

This method can be used to refer an object available in string constant pool which was placed at the time of object creation in heap memory.

Ex:

String s1 = new String(“program”);

String s2 = s1.intern();

System.out.println((s1));

System.out.println((s2));

* If any one of them is of string type it will perform concatenation.

* If both are of numeric type it will perform addition.

class Test{

 public static void main(String[] args){

String str = "hello";

System.out.println(str);

System.out.println(str+1+2+3);

System.out.println(1+str+2+3);

System.out.println(1+2+str+3);

System.out.println(1+2+3+str);

System.out.println(str+(1+2+3));

System.out.println(1+str+(2+3));

System.out.println((1+2)+str+3);

System.out.println((1+2+3)+str);

} O/P: hello

hello123 1hello23 3hello3 6hello hello6 1hello5 3hello3 6hello

* The equals method is used for comparing contents of the string.

* The == operator will compare the reference (Address) of the string.

Ex: class Test{

 public static void main(String[] args){

String s1 = new String("hello");

String s2 = new String("hello");

String s3 = ("hello");

String s4 = ("hello");

System.out.println(s1.equals(s2));

System.out.println(s1.equals(s3));

System.out.println(s3.equals(s4));

System.out.println(s1==s2);

System.out.println(s1==s3);

System.out.println(s3==s4);

} }

O/P: true true true false false false