Arrays

Been so long since I posted, Happy that i'm able to be back with basic concepts again.

Let's see what is Arrays:

As we know Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. It is a fixed-size sequential collection of elements of the same data type.

To declare an array, define the variable type with square brackets:

String[] fruits;

To place the values in like below we can use a comma-separated list, inside curly braces:

String[] fruit={"Apple",Orange","Strawberry"};

we can also directly declare and add values with just above line.


  • Fixed Size: The size must be specified when the array is created and cannot be changed later.

Access the Elements of an Array

You can access an array element by referring to the index number.

This statement accesses the value of the first element in fruits:

String[] fruits={"Apple",Orange","Strawberry"};

System.out.println(fruits[0]);

// Output Apple


  • Homogeneous Elements: All elements must be of the same data type.
  • Random Access: Elements can be accessed directly using indices.


Multidimensional Arrays in Java

A multidimensional array is an array of arrays. In Java, the most common type is the 2D array, but you can also have arrays with more than two dimensions.

An example of 2 dimensional is below:


Let's see few common operations we do with Arrays:

1.Arrays.sort() method for sorting.



2.Arrays.copyOf() Copying an Array:




Using System.arraycopy()



Hope we were able to understand Arrays today, let's see collections tomorrow. Happy Learning.

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了