473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,323 developers and data experts.

Arrays in Java| One D Arrays| 2 D Array |3 D Arrays

Implement One Dimensional Arrays in java
package com.ds.algorithms.array;

import java.util.Scanner;

/**

@Author pankaj
@create 09/04/21 5:14 PM
Note: Arrays are Fixed in Size
Array store only homogeneous element */ public class ImplementOneDArray { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("Plz Enter the Size of Array ???"); int size=scanner.nextInt(); //code to create array with size, given by user int[] array=new int[size]; System.out.println("Plz Enter all "+size+" Value to store in Array"); //code to insert all input element from user for (int i=0;i< size;i++) { array[i]=scanner.nextInt(); } // code to print all arrays element System.out.println("All the values stored in Array is Following"); for (int i=0;i<size;i++) { System.out.println(array[i]); } } } # Implement two Dimensional Arrays in Java package com.ds.algorithms.array;
import java.util.Scanner;

/**

@Author pankaj
@create 4/10/21 11:44 AM
*
In java multi dimensional array is implemented as array of arrays concept or jagged array
Note: In multidimensional array we must need to specify base size, and remaining can be ignored.
/
public class ImplementThreeDArray {
public static void main(String[] args) {
/ #######################################
int [][][] threeDArray =new int[2][][];
threeDArray[0] =new int[3][];
threeDArray[0][0]=new int[1];
threeDArray[0][1]=new int[2];
threeDArray[0][2]=new int[3];

threeDArray[1]=new int[2][2];
#############################################*/

Scanner scanner=new Scanner(System.in);
System.out.println("Plz Enter base siz 3D Array");
int basSize1=scanner.nextInt();
System.out.println("Plz Enter base size of 2D Array");
int basSize2=scanner.nextInt();
System.out.println("Plz Enter base size of 1D Array");
int basSize3=scanner.nextInt();
//create 3 D array
int [][][] threeDArray=new int[basSize1][basSize2][basSize3];
System.out.println("Please Enter all "+basSize1*basSize2*basSize3 +" values foe 3 D Array !!!!");
for (int i=0;i<basSize1;i++)
{
for (int j=0;j<basSize2;j++)
{
for (int k=0;k<basSize3;k++)
{
threeDArray[i][j][k]=scanner.nextInt();
}
}
}
// call method to print values
printThreeDArray(threeDArray);
}

public static void printThreeDArray(int [][][] array)
{

/* for (int i=0;i<array.length;i++)
{
for (int j=0;j<array[i].length;j++)
{
for ( int k=0;k<array[i][j].length;k++)
{
System.out.print(array[i][j][k] + " ");
}
}
System.out.println();
} */
for( int [][] i:array)
{
for(int [] j: i)
{
for( int k: j)
{
System.out.print(k+" ");
}
System.out.println();
}
System.out.println();
}
}
}

code to Add Two Dimensional Array || Add two Matrix
package com.ds.algorithms.array;

/**

@Author pankaj
@create 09/04/21 6:28 PM */ public class AdditionOfTwoDMertices { public static void main(String[] args) { //array 1 int [][] array1={ {2,2,2}, {3,3,3}, {4,4,4,} }; //array 2 int [][] array2={ {2,2,2}, {5,5,5}, {6,6,6,} }; System.out.println("Print array first"); ImplementTwoDArray.printTwoDArray(array1); System.out.println("print array second"); ImplementTwoDArray.printTwoDArray(array2); // create new array to store addition of array int [] [] resultArray=new int[array1.length][array1.length]; // logic for addition for (int i=0;i
/**

@Author pankaj
@create 09/04/21 5:51 PM
*/
public class ImplementTwoDArray {
public static void main(String[] args) {
//way 1
int [][] empSalary={
//emp sal department wise,sales,testing,dev
// 1 1 3 4 5
{44444,654,897,896347,7478456},
{5845,34,234,3645,876,78687,4545,435535},
{56645566,654646,4564666,12578,45564,3434,34435,53 42,234}
};
// print all emp salary
System.out.println("+++++++++++++ print all emp salary ++++++++++++++++");
printTwoDArray(empSalary);
// way 2
int [][] twoDArray=new int[3][4];
// 3 one D array with 4 elements
System.out.println("========== print 2d array with default values==========");
printTwoDArray(twoDArray);

}
public static void printTwoDArray(int [][] array){
for (int i = 0; i < array.length; i++) // 0 to 2
{
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Apr 11 '21 #1
0 3400

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: johnny | last post by:
hello. in C++, you can fake a sub-array by passing something like (array + int). If this was passed to something that takes an array as a parameter, the function will deal with the array using...
2
by: harborcoat-design | last post by:
Given the code below I think I should get an alert message box that says "one" when the link is clicked - but instead i get an error that "x.1 is null or not an object" Thanks for any help -...
1
by: rir3760 | last post by:
Since a few days ago I have been working with the program I post below (a school assignment). The purpose of the program is to work with the va_ macros (stdarg.h) and arrays of arrays, hopefully...
4
by: k04jg02 | last post by:
I was thinking that it would be cool if a programming language could implement an array type in its standard library rather than depending on arrays being a builtin type. I wondered if C++ could do...
11
by: unholydreadlord | last post by:
Hi im new to java and i am currently trying to use arrays. Can someone help me find a way to input a number into the array and then push the number to the next slot if you input another number. The...
3
by: monkey1001 | last post by:
how do i write a java program tht will store seven integer values in an array of type integer......please helppppp... this is what i got but not sure about it...thnkss...and it has to include reverse...
1
by: gypsyman58 | last post by:
First I need to create 2 classes. The first will contain an ID number and an array of 5 course titles. I also have to create a get and a set method for populating the ID. I also must create a method...
14
by: spreadbetting | last post by:
I'm trying to split a string into an separate arrays but the data is only delimited by a comma. The actual data is one long string but the info is in a regular format and repeats after every five...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.