banner



How To Create An Array From A Text File Java

2D Arrays in Java

Overview of 2D Arrays in Java

The following article, 2D Arrays in Java, provides an outline for the creation of 2D arrays in java. An array is one of the data types in java. An array is a group of homogeneous data items which has a common name. The array consists of data of any data type. 2-dimensional array structured as a matrix. Matrix is a combination of rows and columns.

Example

int apple[20];

or

char fullName[] = new char[10];

Arrays should use a single data type; it will be int or string or something else.

Before going forward, we have to know why we need an array. Suppose we have data of the same type. Like we have 5 employees, and we need to save those five employees, we can create five different variables to save that data. But what if we have 1000 employees. Here the name of the employee is of string type. We cannot create 1000 variables to save this data. It is very time-consuming and tedious. So the solution is an array. We will create only one variable of type array, and we will give a size of 100.

Example

string employee[100];

Types of 2D Arrays in Java

There are two types of arrays in java. Please look at the following diagram:

Types of Arrays

1. Single Dimensional Array

A single or one-dimensional array means it has only one value for every index. i.e. memory location.

Example

employee[3]

2D Arrays in Java

In the above diagram, we have 3 employees List. And consider we have only one data like employee name. This is called a single-dimensional array.

2. Multidimensional Array

Now come to a multidimensional array. We can say that a 2d array is an array of array. A multidimensional array is mostly used to store a table-like structure.

In today's topic, we are going to see this 2-dimensional array. 2 dimensional Array has two pairs of square brackets. The first one for the row and the second one for the column. Remember that if we need to get any value in the array, we are using the index number associated with it. Look at the following diagram for a clear understanding

Example

this is for a one-dimensional array

One Dimensional Array

Also, for a 2-dimensional array, look at the following diagram.

2 Dimensional Array

How 2D Arrays Defined in Java?

There are some steps involved while creating two-dimensional arrays.

  1. Declaring a 2d array
  2. Creating the object of a 2d array
  3. Initializing 2d array.

Now we will overlook briefly how a 2d array gets created and works.

1. Declaring 2 Dimensional Array

Syntax: there are two forms of declaring an array.

Type arrayname[];

Or

type[] array name;

Look at the following examples

Example

int name[][];

or

int[][] name;

2. Creating an Object of a 2d Array

Now, it's time to create the object of a 2d array.

name = new int[3][3]

Creating a 2-dimensional object with 3 rows and 3 columns.

3. Initializing 2d Array

After creating an array object, it is time to initialize it.

In the following code, we describe how to initialize the 2-dimensional array.

Int name[3][3] = {"a","b","c","a1","b1","c1","a2","b2","c2"};

OR

int name[3][3] = {{"a","b","c"},
{"a1","b1","c1"},
{"a2","b2","c2"}};

 How to Create 2D Arrays in Java?

We will look at how to create 2 dimensional with the help of an example. Before that, let us look, we have two index values for a 2d array. One is for a row, and another is for the column.

Row Size

Rows are the elements in an array that can store horizontally. For example, Row Size is equal to 4, then the array will create with 4 rows.

Column Size

Columns are the elements in an array that can store vertically. For example, Column Size is equal to 2, then an array that can have 2 Columns in it.

public class TwoDArray{
public static void main(String[] args) {
int[][] twoDimentional = {{1,1},{2,2},{3,3},{4,4}};
for(int i = 0 ; i < 4 ; i++){
for(int j = 0 ; j < 2; j++){
System.out.print(twoDimentional[i][j] + " ");
}
System.out.println();
}
}
}

Output:

Create 2D Arrays in Java

In the above program, we have defined a 2d array. We have an array, and we printed values in that array as a table-like structure. If you are clear about the basic concepts like for loop, you can easily understand the above program. Try to write and run the above code. This will lead you to understand it more quickly.

How to Insert Elements of 2D Arrays in Java?

Till now, we have seen types of the array and what id 2d array. Now we need to explore more about this. Let's go one step further. We have given an array, and in that array, we need to add some values. How can we achieve this?

For inserting data In 2d arrays, we need two for loops because we are working with rows and columns here.

  1. Ask for an element position to insert the element in an array.
  2. Ask for value to insert
  3. Insert the value
  4. Increase the array counter

All the things mentioned above can be confusing. Let's look at the below program, which illustrates the way to take user input in a 2d array.

Please try out this program first. We will have a closer look at the below program.

Example

import java.util.Scanner;
public class InsArray{
public static void main(String[] args)
{
int[][] twodArray = new int[3][2]; // declared and created array object
Scanner s1 = new Scanner(System.in); //created Scanner object
System.out.println("Please enter the values to be added");
for(int i = 0 ; i < 3 ; i++){
for(int j = 0 ; j < 2; j++)
{
twodArray[i][j] = s1.nextInt();
}
System.out.println();
}
System.out.println("Your output would be as below:");
for(int i = 0 ; i < 3 ; i++){
for(int j = 0 ; j < 2; j++)
{
System.out.print(twodArray[i][j] + " " );
}
System.out.println();
}
}
}

Output:

Please enter the values to be added

22
11
22
33
44
55

Your output would be as below:

22 11
22 33
44 55

In the above program, we have taken one array variable called twodArray. We just created the object of an array. We have not initialized this array yet. For taking user input, we took the help of a scanner class in java. We created the object of this class called s1. We created this object to use different methods specified in a class scanner.

Further, we used the nextInt() method in the scanner class to take input from the user at a particular location.

Here, we used nested for loops to loop over row and column. The first nesting set takes input from the user, which is nothing but the inserting values in a 2-dimensional array. The second nesting of for loop is to display user input on the screen in a matrix format.

This is a very simple program to understand. Suppose you are having trouble understanding nested for loop. Please learn first how for loop works in java. Then try again.

How to Update Elements of 2D Arrays in Java?

Till now, we have seen how to insert elements in a 2d array. Now let's check how we can update the existing 2d array. To update elements in a 2-dimensional array, we need to see which element we have to update. If you are familiar with array concepts, you know that we have an index number to each element; we can say the position in short. Let's first jump on to the program, and later we will see what actually we are doing with this.

Example

public class UpArray{
public static void main (String[] args)
{
String[][] twoDimentional = {{"1","1"},{"2","2"},{"3","3"},{"4","4"}};
System.out.println("Before updating an array: ");
printArray(twoDimentional);
twoDimentional[3][0] = "5";
System.out.println("After updating an array element: ");
printArray(twoDimentional);
}
private static void printArray(String[][] twoDimentional){
for(int i=0; i<twoDimentional.length; i++){
for(int j=0; j<twoDimentional[0].length; j++){
System.out.print(twoDimentional[i][j]);
}
System.out.println("");
}
}
}

Output

Before updating

In the above program, we have updated the value in the 2-dimensional array. We have an array named two-dimensional. We have values as {{"1″,"1"},{"2″,"2"},{"3″,"3"},{"4″,"4"}}. We know that a 2d array is an array of arrays. Here we tried to update the value of the 4th array. We took the value by its index position. In array, we know that the index starts at 0th. So the array index would be 3. And the first position in the array means the 0th position. So it would be [3][0].we are assigning a new value at the given position, i.e. [3][0]. That value is five. You can see in the output above previously 44 was there; after updating 51 is there.

How to Remove Elements?

  • Now, it's time to see if we need to remove some particular elements in 2d array. How can we achieve this?
  • Now, this is the tricky question asked many times. But we need to understand that in java, we can't delete an item in 2d arrays. 2-dimensional arrays are nothing but an array of arrays. But there is a way to remove that element via replacing the places.
  • With all these possibilities, there are some disadvantages also over the array, as we have a fixed size. Java also has a java collection framework. This collection framework has an Array List, which is also the technique to work with different collections of java.

Conclusion

2d arrays are part of arrays. This is very important to know how the multidimensional array works. Arrays are the core concept in java programming. If you really want to be good at Java, you should work on arrays.

Recommended Articles

This is a guide to 2D Arrays in Java. Here we discuss the introduction to 2D Arrays in Java along with how to create, insert, update and remove elements. You may also look at the following articles to learn more –

  1. 2-D Arrays in C
  2. 2D Arrays in C#
  3. Arrays in PHP
  4. 2D Graphics in Java

How To Create An Array From A Text File Java

Source: https://www.educba.com/2d-arrays-in-java/

Posted by: alcantarlexiskings.blogspot.com

0 Response to "How To Create An Array From A Text File Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel