Lecture 11: Arrays in Java
Arrays in Java:
4.1 Introduction of Array:
An array is a group of contiguous or related data items that share a common name. An array is a collection of similar type of elements which have a contiguous memory location in general.
Java array is an object which contains elements of a similar data type. Additionally, the elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, and 2nd element is stored on 1st index and so on.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java.
4.2 Types of arrays in Java:
1. One dimensional array:
2. Multi-dimensional array:
1. One dimensional array:
• General form of an one-dimensional array declaration is as follows:
type var_name[];
e.g. int month_days[];
• Here month_days is an array variable, no actually array exists. In fact, value of month_days is set to null, which represents an array with no value.
• To link, month_days with an actual, physical array of integers, you must allocate one using new keyword.
• new is special operator that allocates memory at runtime on heap memory.
e.g.
int number[] = new int[5];
this statement new keyword reserves five integer type block of memory on heap and returns the base address of the block and that address or reference is stored in reference variable number.
Creation of an array involves three steps:
1. Declare array
2. Create memory locations
3. Initialise values into memory locations
1. Declaration of array:
type arrayname[];
type []arrayname;
e.g.
int number[];
int [] counter;
Note: Do not enter size of array in declaration in Java because subscript operator is only for showing that number variable is array type but in C and Cpp at the time of declaration size is must specified otherwise it gives you error at compile time.
2. Creating memory locations:
int a = 5;
number = new int [a];
int number [];
number = new int [3];
3. Initialise values into memory locations:
number[0]=21;
number[1]=31;
number[2]=41;
Unlike C and Cpp, Java protects arrays from bound overflow or bounds underflow. In Java, if array index get out of bound then it is compile time error. That is array bound is strictly type checked in Java.
int a [] = {1,2,3};
int b [];
b = a;
are valid in Java. Then both arrays will have same values. Here one reference variable is assign to other because both having same type. That is a and b reference variables are pointing to same location.
array length:
In java, all arrays store the allocated size in a variable named length.
we can access the length of array a using a.length.
Please like comment share and subscribe.
Prof. Amol M. Jagtap
For more contact : 7972761065
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Lecture 11: Arrays in Java», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.