472,976 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,976 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 3345

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.