Uint-I(Array,String,Vector) (Autosaved)
Uint-I(Array,String,Vector) (Autosaved)
An Array, one of the data structures in Java, is a collection of variables of the same type that are
referenced by a common name. Arrays consist of contiguous memory locations. The first address
of the array belongs to the first element and the last address to the last element of the array.
Arrays can have data items of simple types such as int or float, or even user-defined data
types like structures and objects.
Arrays are considered as objects in Java.
The indexing of the variable in an array starts from 0.
Like other variables in Java, an array must be defined before it can be used to store
information.
We can find the length of arrays using the member ‘length’.
The size of an array must be an int value.
Object class is a super class of the Array.
Array implements the two interfaces: Serializable and Cloneable.
The need for an Array:-
Arrays are very useful in cases where many elements of the same data types need to be stored
and processed. It is also useful in real-time projects to collect the same type of objects and send
all the values with a single method call.
A single-dimensional array in Java is a linear collection of elements of the same data type.
1. Array Declaration
To declare an array in Java, use the following syntax:
type[] arrayName; or type[] arrayName;
type: The data type of the array elements (e.g., int, String).
arrayName: The name of the array.
Note: The array is not yet initialized.
2. Create an Array
To create an array, you need to allocate memory for it using the new keyword:
// Creating an array of 5 integers
numbers = new int[5];
We can access array elements using their index, which starts from 0:
The first line sets the value of the first element to 10. The second line retrieves the value of the
first element.
5. Array Length
Array Properties
In Java, all arrays are dynamically allocated.
Since arrays are objects in Java, we can find their length using the object property length.
This is different from C/C++, where we find length using size of.
A Java array variable can also be declared like other variables with [] after the data type.
The variables in the array are ordered, and each has an index beginning with 0.
Java array can also be used as a static field, a local variable, or a method parameter.
An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a
class, depending on the definition of the array. In the case of primitive data types, the actual
values might be stored in contiguous memory locations (JVM does not guarantee this behavior).
In the case of class objects, the actual objects are stored in a heap segment.
In a situation where the size of the array and variables of the array are already known, array
literals can be used.
The length of this array determines the length of the created array.
There is no need to write the new int[] part in the latest versions of Java.
Now , we have created an Array with or without the values stored in it. Access becomes an
important part to operate over the values mentioned within the array indexes using the points
mentioned below:
All the elements of array can be accessed using Java for Loop.
class ArrayDemo {
public static void main(String[] args)
int[] arr;
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Output:-
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
class Small {
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
min(a);
}}
0
Memory Representation of single-dimensional
arrays
Single-dimensional arrays are lists of data of the same type and their
elements are stored in a contiguous memory location in their index order. For
instance, an array age of type int with 5 elements is declared as:
Or as,
This array will have age[0] at the first allocated memory location, age[1] at the
next memory location and so on. Since age is an int type array, the size of
each element is 4 bytes. If the starting memory location of array age is 5000,
then it will be represented in the memory location as follows:
Ar
rayIndexOutOfBoundsException
In Java, the ArrayIndexOutOfBoundsException is a runtime exception thrown by the Java
Virtual Machine (JVM) when attempting to access an invalid index of an array. This
exception occurs if the index is negative, equal to the size of the array, or greater than the
size of the array while traversing the array.
Output:-
2. Multi-dimensional arrays
Multi-dimensional arrays are arrays of arrays in which each element of the array holds the
reference of other arrays. We can also call them Jagged Arrays. A multidimensional array in
Java is an array of arrays where each element can be an array itself. It is useful for storing
data in row and column format.
Example:-
The array myArray has 5 elements myArray [0], myArray[1], myArray[3], and myArray[4], each of
which itself is an int array with 12 elements.
class TwoD{
myArray[0][0]=1;
myArray[0][1]=2;
myArray[0][2]=3;
myArray[1][0]=4;
myArray[1][1]=5;
myArray[1][2]=6;
myArray[2][0]=7;
myArray[2][1]=8;
myArray[2][2]=9;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}}
Output:-
123
456
789
int rows=11;
int cols=11;
int product[][]=new int[rows][cols];
System.out.println("Multiplication Table");
System.out.println();
int i,j;
for(i=1;i<rows;i++)
{
for(j=1;j<cols;j++)
{
product[i][j]=i*j;
System.out.print("\t "+product[i][j]);
}
System.out.println(" ");
}
}
}
Output:-
Multiplication Table
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
copyOf(originalArray, newLength)
We can copy the specified array to a new array with a specified length. The left spaces are
assigned to default values in the new array.
sort(originalArray)
Sorts the complete array in ascending order.
toString (original array):-It returns a string representation of the contents
of this array.
Java String
Basically, string is a sequence of characters but it’s not a primitive type.
String is immutable object which means that it cannot be changed once it is created.
String is the only class where operator overloading is supported in java. We can concat
two strings using + operator. For example "a"+"b"="ab".
example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
Character Class
String Class
It is a class whose instances or objects can hold the unchanging or constant string (immutable
String).
StringBuffer Class
Its instances or objects can hold mutable strings that can be changed or modified.
StringBuilder Class
Java StringBuilder class is similar to the StringBuffer class which holds a sequence of
mutable Strings but is more versatile than StringBuffer class because it provides more
modification features.
How to create a string object?
There are two ways to create String object:
1. By string literal
2. By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
String s="welcome";
By new keyword
String s=new String("Welcome");//creates two objects and one reference variable
Example: Create a String in Java
String Length
Methods used to obtain information about an object are known as accessor methods. One
accessor method that you can use with strings is the length() method, which returns the number
of characters contained in the string object.
System.out.println("s1+s2:- "+s1.concat(s2));
Output:-
String s1 length:- 27
String s2 length:- 16
String s1== s2:- false
Vector:
What is Vector in Java?
Vector is a data structure that is used to store a collection of elements. Elements can be
of all primitive types like int, float, Object, etc. Vectors are dynamic in nature and
accordingly, grow or shrink as per the requirement.
Vector Class in Java is found in the java.util package.
Vector class is a child class of the AbstractList class and implements the List interface.
Therefore we can use all the methods of the List interface.
Vectors are known to give ConcurrentModificationException when accessed
concurrently at the time of modification.
When a Vector is created, it has a certain capacity to store elements that can be defined
initially. This capacity is dynamic in nature and can be increased or decreased.
By definition, Vectors are synchronized, which implies that at a time, only one thread is
able to access the code while other threads have to wait. Due to this, Vectors are slower
in performance as they acquire a lock on a thread.
Apart from the methods inherited from its parent classes, Vector
defines the following methods –