JAVA: Array, Strings, Vectors

Ritik Sahu
4 min readApr 12, 2019

--

By — Ritik Sahu

- Arrays

An array is a block of container that can hold data of one single type. The array is a fundamental construct in Java that allows you to store and access a large number of values conveniently.

How to declare an array?

Here’s how you can declare an array in Java:

dataType[] arrayName;

the dataType can be a primitive data type like int, char, Double, byte, etc.

arrayName is an identifier.

Let’s take the above example again.

Double[] data;

Here, data is an array that can hold values of type Double.

But, how many elements can array this hold?

Good question! We haven’t defined it yet. The next step is to allocate memory for array elements.

data = new Double[10];

The length of the data array is 10. Meaning, it can hold 10 elements (10 Double values in this case).

Note, once the length of the array is defined, it cannot be changed in the program.

Strings

Basically, the string is a sequence of characters but it’s not a primitive type.

When we create a string in java, it actually creates an object of type String.

The string is an immutable object which means that it cannot be changed once it is created.

The string is the only class where operator overloading is supported in Java. We can concat two strings using + operator. For example “A”+”B”=”AB”.

Java provides two useful classes for String manipulation — StringBuffer and StringBuilder.

Let’s go ahead and learn more about the Java String class.

Different Ways to Create String

There are many ways to create a string object in java, some of the popular ones are given below.

Using string literal

String name = “Ritik”;

Using a new keyword

String name = new String(“ritik”);

char[] ch = {‘r’, ‘i’, ‘t’, ‘i’, ‘k’};

String name2 = new String(ch);

string2 = string1.toLowerCase;

This statement converts all the Characters of string1 into Lowercase Letters. Its output is obtained as follows-

Different Ways to Create String

Let’s take an example

“Ritik”

string2 = string1.toUpperCase;

This statement converts all the Characters of string1 into Uppercase Letters. Its output is obtained as follows-

“RITIK”

string2 = string1.replace (‘k’, ‘i’);

This statement replaces all ‘a’ Characters located in string1, replacing Character ‘i’. Its output is obtained as follows-

“Ritii”

string2 = string1.trim ();

This statement eliminates all the White Space characters that came before and after the string1. Its output is obtained as follows-

“Ritik”

String1.equals (strin2);

If both Strings Objects have the same String, then this statement returns true Return. For example, if string1 = “Rahul” and string2 = “Rahul”, then this statement will return true.

string1.length ();

This statement returns the length of string1. If string = “Ritik”, then this statement returns 5, because there are five characters in this string.

Vector

Java Vector class comes under the java.util package. The vector class implements a growable array of objects. Like an array, it contains the component that can be accessed using an integer index.

Vector is very useful if we don’t know the size of an array in advance or we need one that can change the size over the lifetime of a program.

Variable size

Java Vector Class Declaration

public class Vector<E>

extends Object<E>

implements List<E>, Cloneable, Serializable

The following are the list of vector class methods:

1) add() — It is used to append the specified element in the given vector.

2) addAll() — It is used to append all of the elements in the specified collection to the end of this Vector.

3) addElement() — It is used to append the specified component to the end of this vector. It increases the vector size by one.

4) capacity() — It is used to get the current capacity of this vector.

5) clear() — It is used to delete all of the elements from this vector.

6) clone() — It returns a clone of this vector.

7) contains() — It returns true if the vector contains the specified element.

8) containsAll() — It returns true if the vector contains all of the elements in the specified collection.

9) copyInto() — It is used to copy the components of the vector into the specified array.

10) elementAt() — It is used to get the component at the specified index.

--

--

No responses yet