Java Program for Concatenation of Array.

Given an integer array nums of length n, create a new array ans of length 2n such that ans is the concatenation of two nums arrays. 

Example:
Input: nums = [1, 2, 4]
Output: ans = [1, 2, 4, 1, 2, 4]

Input: nums = [1, 3, 2, 1]
Output: ans = [1, 3, 2, 1, 1, 3, 2, 1]

Steps:

  • Create a new array ans[] = new int[2*n] of size 2n where n is the size of the given array. 
  • Copy the elements given into the new array where ans[i] = nums[i] and ans[i+n] = nums[i]
  • Return ans.
public class Main
{
    public static int[] getConcatenation(int[] nums) {
        
        int ans[] = new int[2*nums.length];
        
        for(int i = 0; i<nums.length; i++){
            ans[i] = nums[i];
            ans[i+nums.length] = nums[i];
        }
        return ans;
    }
public static void main(String[] args) {
    int nums[] = {1, 2, 1, 3};
    
    int[] ans = getConcatenation(nums);
    
    for(int i = 0; i<ans.length; i++) {
        System.out.print(ans[i]+" ");
    }
}
}
Output:
1 2 1 3 1 2 1 3

Java Programming Complete Tutorial

Java Programming

The Java Programming Language is a general-purpose, high-level, strongly-typed, class-based object-oriented programming language. All Java applications first complied with the bytecode that can run on any Java Virtual Machine (JVM) regardless of the underlying computer architecture. 
 
Introduction to Programming:

Basic of Java:

Access Specifiers in Java.

The process of hiding information is implemented using Access Specifiers known as visibility modifiers. They regulate access to classes, fields, and methods. Access Specifiers specify if fields or methods in a class can be invoked from another class or sub-class. In simple words, they are used to restrict access. 

There are four types of Access Specifiers in Java. 

  • Public
  • Private
  • Protected
  • Default (no specifier)
Public: Classes, methods, and fields declared as public can be accessed from any class in an application. 
Default: If Classes, variables, and methods have a default specifier we can access them only within the same package but not from outside this package. 
When no access specifiers are specified for an element, its accessibility level is the default. There is no keyword default. (alert-passed)
Private: Private methods and fields can only be accessed within the same class to which the methods and field belong. 
Protected: Protected methods and fields are accessible within the package and outside the package but with the help of inheritance. It cannot be applied to a class and it provides more accessibility than the default access specifier.    


Access Modifiers default private protected public
Accessible inside the class Yes Yes Yes Yes
Accessible within the subclass inside the same package Yes No Yes Yes
Accessible outside the package No No No Yes
Accessible within the subclass outside the package No No Yes Yes

Accessors and Mutators in Java.

Java accessor and mutaotrs

Accessors and Mutators are two very important concepts in Java. It is used to read and update the private data member of a class. 

To understand this concept more clearly we first have to understand encapsulation and access specifiers. Encapsulation is an important concept in Object-oriented programming and it is implemented by wrapping up the data and methods into a single unit. 

The data present inside the class need to be secure and for this, we declare the data in a class as private. We cannot access the private data-member of other classes outside that class. To read and update the private data member a class outside that class we have to use public methods of that class. These methods are called Accessors or Getters and Mutators or Setters

Accessor or Getters are methods used to return the value of a private field of a class. It does not change the state of the Object. 

example:

public String getName() {
    return name; 
}    

Mutators or Setters are methods used to set a value of a private field of a class. It changes the state of the object. 

example:

public void setName(String fullName) {
    name = fullName;
}    


Example Java Program to Understand Accessors and Mutators.

public class Student {
    
    //attribute
    private int studId;
    private String studName;
    
    //Accessors or Getters
    public int getStudId() {
        return studId;
    }
    public String getStudName() {
        return studName;
    }
    //Mutators or Setters
    public void setStudId(int studId) {
        this.studId = studId;
    }
    public void setStudName(String studName) {
        this.studName = studName;
    }
}

import java.util.*;

public class Main
{
	public static void main(String[] args) {
	    //Object Creation
	    Student Obj = new Student();
	    
	    //assign value using Setters
	    Obj.setStudId(112);
	    Obj.setStudName("Rahul");
	    
	    //retrive value using Getters
	    System.out.println("Student ID: "+Obj.getStudId());
	    System.out.println("Student Name: "+Obj.getStudName());
	}
}
Output:
Student ID: 112
Student Name: Rahul

In the above example, we can see that how we are reading and updating the private data member of the Student class from the Main class with the help of the Object of Student class. 

Hope you found this article useful if you have any question related to this topic feel free to ask me in the comment section below. 

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by templateszoo