DEV Community

Cover image for One Commit at a Time ! Series - 10
Annapoorani Kadhiravan
Annapoorani Kadhiravan

Posted on

One Commit at a Time ! Series - 10

JAVA

Copying Arrays Using arraycopy() method
In Java, the System class contains a method named arraycopy() to copy arrays. This method is a better approach to copy arrays than the above two.

The arraycopy() method allows you to copy a specified portion of the source array to the destination array. For example,

arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
Here,

src - source array you want to copy
srcPos - starting position (index) in the source array
dest - destination array where elements will be copied from the source
destPos - starting position (index) in the destination array
length - number of elements to copy
Let's take an example:

// To use Arrays.toString() method
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        int[] n1 = {2, 3, 12, 4, 12, -2};

        int[] n3 = new int[5];

        // Creating n2 array of having length of n1 array
        int[] n2 = new int[n1.length];

        // copying entire n1 array to n2
        System.arraycopy(n1, 0, n2, 0, n1.length);
        System.out.println("n2 = " + Arrays.toString(n2));  

        // copying elements from index 2 on n1 array
        // copying element to index 1 of n3 array
        // 2 elements will be copied
        System.arraycopy(n1, 2, n3, 1, 2);
        System.out.println("n3 = " + Arrays.toString(n3));  
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

n2 = [2, 3, 12, 4, 12, -2]
n3 = [0, 12, 4, 0, 0]
In the above example, we have used the arraycopy() method,

System.arraycopy(n1, 0, n2, 0, n1.length) - entire elements from the n1 array are copied to n2 array
System.arraycopy(n1, 2, n3, 1, 2) - 2 elements of the n1 array starting from index 2 are copied to the index starting from 1 of the n3 array
As you can see, the default initial value of elements of an int type array is 0.


SQL

BETWEEN Operator
The BETWEEN operator is used in the WHERE clause to select values within a specified range.

The range is inclusive - the beginning and end values of the range are included in the results.

The values can be numbers, text, or dates.

Example:
Select all products with a price between 10 and 20:

SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Enter fullscreen mode Exit fullscreen mode

References:

https://www.programiz.com/java-programming/copy-arrays
https://www.w3schools.com/sql/sql_between.asp

Top comments (0)