Skip to main content

Java Program to see how to use Arrays- Example0003

Java Program to see how to use Arrays- Example0003
Example0003
===========
Description:
In this example we learn to create Arrays in different 
We will also see lots of methods like sort, length.
We will see how to initialize data in the array.
We will see how to display content of single dimension and multidimensional arrays.


//Arrays

package com.swprogramdeveloper;

import java.awt.*;
import java.util.Arrays;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
// write your code here
    //Arrays
    int[] numbers1 = new int[5];
    numbers1[0] = 5;
    numbers1[3] = 9;
        System.out.println("Address where valuese of numbers1 array stored : " +  numbers1);
        System.out.println("Size of Array numbers1 : " + numbers1.length);
        System.out.println("Content of Array numbers1 : " + Arrays.toString(numbers1));
        System.out.println("Content of certain member of Array : " + numbers1[3]);
       
// sorting content
        Arrays.sort(numbers1);
        System.out.println("Content of Array numbers1 - Sorted : " + Arrays.toString(numbers1));
       
// Initializing array in easier way
        int[] numbers2 = {2,6,9,1,3,0,9};
        System.out.println("Content of array numbers2 : " + Arrays.toString(numbers2));
       
// Multidimensional array
        //any dimension we declare as int [][] - 2 box brackets
        int [][] numbers3 = {{1,2,3},{3,5,6}};
        System.out.println("Address of 2-dimensional Array" + Arrays.toString(numbers3));
       
int [][] numbers4 = {{1,2,3},{3,5,6},{0,3,4}};
        System.out.println("Address of 3-dimensional Array" + Arrays.toString(numbers4));
        System.out.println("Content of 3-dimensional Array" + Arrays.deepToString(numbers4));
       
int [][] numbers5 ={{1,2,3},{1,2},{3,4}};
        System.out.println("Address of 3-dimensional Array- separate initialization" + Arrays.toString(numbers5));
        System.out.println("Address of 3-dimensional Array- separate initialization" + Arrays.deepToString(numbers5));
       
int [][][] numbers6 = new int [3][2][4];
        numbers6[0][0][1]= 1;
        numbers6[0][1][0]=10;
        numbers6[2][0][0] = 300;
        System.out.println("Address of 3-dimensional Array- separate initialization" + Arrays.toString(numbers6));
        System.out.println("Address of 3-dimensional Array- separate initialization" + Arrays.deepToString(numbers6));
    }
}


Output0003
==========

Address where valuese of numbers1 array stored : [I@10f87f48
Size of Array numbers1 : 5
Content of Array numbers1 : [5, 0, 0, 9, 0]
Content of certain member of Array : 9
Content of Array numbers1 - Sorted : [0, 0, 0, 5, 9]
Content of array numbers2 : [2, 6, 9, 1, 3, 0, 9]
Address of 2-dimensional Array[[I@5b6f7412, [I@27973e9b]
Address of 3-dimensional Array[[I@312b1dae, [I@7530d0a, [I@27bc2616]
Content of 3-dimensional Array[[1, 2, 3], [3, 5, 6], [0, 3, 4]]
Address of 3-dimensional Array- separate initialization[[I@506e1b77, [I@4fca772d, [I@9807454]
Address of 3-dimensional Array- separate initialization[[1, 2, 3], [1, 2], [3, 4]]
Address of 3-dimensional Array- separate initialization[[[I@1ddc4ec2, [[I@133314b, [[I@b1bc7ed]
Address of 3-dimensional Array- separate initialization[[[0, 1, 0, 0], [10, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]], [[300, 0, 0, 0], [0, 0, 0, 0]]]

Process finished with exit code 0

Comments

Popular posts from this blog

Basic Java Program Various Variables and Point - Example0002

Basic Java Program Various Variables and Point - Example 0002 This example is same as Example1 but with added Class Point Using Point - (It is to store x and y coordinates of a point) This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.awt.*; import java.util.Date; public class Main {     public static void main(String[] args) {         // write your code here         //variable types double and intege r         double age = 30;         int herage = (int) age + 20;         //Printing output using System Class         Sy...

Creating Class with attributes and method and using them- Example0006

Creating Class with attributes and method and using them- Example0006 This example shows creating own Class We will create a class called 'Product' with Attribute, Contructor and Methods We will see how we can create an Object of that Class and use the methods in that Class.  We can see two ways to access the attributes of the Class Keyword: java, class, attribute, constructor, methods, object, instance Creating own Class and use it ====================== package com.swprogramdeveloper; //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Constructor         Product() {         System.out.printl...

Basic Java Program with Variable Introduction and Class Introduction - Example0001

Basic Java Program with Variable Introduction and Class Introduction - Example0001  Example1: ========= This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.util.Date; public class Main {     public static void main(String[] args) {  // write your code here      //variable types double and integer         double age = 30;         int herage = (int) age + 20;   //Printing output using System Class         System.out.println("Hello World "+age + "---" + herage);      //float type. Note the F- it is required as the compiler will take doub...