How to set an int array to another int array - java

How could I set an int[] array to another int[]?
Example:
int array[] = new int{1, 2, 3, 4, 5};
int array2[] = new int[]array;
or
int array[] = new int{1, 2, 3, 4, 5};
int array2[] = array[];
But it doesn't work!
Can somebody tell me how?

Why didn't you try with the most obvious:
int[] array2 = array;

You can try to use
array2 = Arrays.copyOf(array, array.length);
From the Java docs:
copyOf
Copies the specified array, truncating or padding with zeros (if
necessary) so the copy has the specified length. For all indices that
are valid in both the original array and the copy, the two arrays will
contain identical values. For any indices that are valid in the copy
but not the original, the copy will contain 0. Such indices will exist
if and only if the specified length is greater than that of the
original array.

Related

Array copying confusion

int[] arr = {1, 2, 3, 4, 5};
int[] copy = arr;
copy[4] = 2;
System.out.println(arr[4]);
So it prints out 2 but I don't know why it would do that when arr doesn't equal copy. Shouldn't it still be 5 or am I dumb?
So it prints out 2 but I don't know why it would do that when arr
doesn't equal copy. Shouldn't it still be 5?
No, this is the expected behaviour. This is because copy and arr are referring to the same array object.
Create a copy in an immutable way as follows:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(arr, arr.length);
copy[4] = 2;
System.out.println(arr[4]);
System.out.println(copy[4]);
int[] anotherCopy = arr.clone();
anotherCopy[4] = 2;
System.out.println(arr[4]);
System.out.println(anotherCopy[4]);
}
}
Output:
5
2
5
2
Instead of making a separate array, your compiler is just assigning a pointer to the the original array. In other words, both of the arrays are actually the same data underneath but with different names and pointers. A change to one will affect the other.

Copy an array into the end of a larger array

I want to copy an array into a larger array, but instead of starting at the first element or a specified element, such as System.arraycopy(), I want it to start at the last element and move backward.
int [] ary1 = new int[5] {1,2,3,4,5};
int [] ary2 = new int [10];
and I want the elements in ary2 to equal
0,0,0,0,0,1,2,3,4,5
or
null,null,null,null,null,1,2,3,4,5
You can use the method System.arrayCopy() for that purpose. Since you use an array of primitive int type, the resulting array will be [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
System.arraycopy(ary1, 0, ary2, 5, ary1.length);
The arguments are:
Source-array
Start-position in source-array
Destination-array
Start-position in destination-array
Number of elements to copy
This will work
int [] ary1 = {1,2,3,4,5};
int [] ary2 = new int [10];
int y = ary2.length-1;
for (int i = ary1.length-1; i >=0; i--) {
ary2[y]=ary1[i];
y--;
}

Can I create more columns in the array 2D?

I want to create 2 cells in the first array, but in the second array, I want to have 4 cells.
Is that possible in Java and is it logical?
Example:
public void stack(){
int a[][] = {{2,5234},{5,33,345,45}};
}
Yes this is possible:
int[][] a = new int[2][];
a[0] = {2, 5234};
a[1] = {5, 33, 345, 45};
Or if you want to inline the entire definition:
int[][] a = new int[][]{{2, 5234}, {5, 33, 345, 45}};
This is known as a "jagged array." If you wanted to declare the sizes of these, you could start off with the 2D array declaration:
int[][] a = new int[2][];
and then you can make the length whatever you please
a[0] = new int[2];
a[1] = new int[4];
Essentially the second dimensions of the arrays are arrays themselves. You can write explicitly what you want as:
a[0] = [2, 5234];
a[1] = [5, 33, 345, 45];
And furthermore, you can access the length of the second dimensions with:
int lengthOne = a[0].length;
int lengthTwo = a[1].length;
Is it logical? Absolutely. Jagged arrays are highly useful for things which don't fit the classic table format such as recording temperatures. This could be recorded as (for example):
double[][] temperatures = new double[12][];
Where there are 12 months in a year and:
temperatures[0] = new double[31];
January has 31 days.

Using a for loop to assign an array to another array

I have a project where by I used a sorting algorithm to sort an array but I am at the point where I now need to examine different arrays of different sizes and different values. Is there a way I can assign an array to a global array using a for loop
e.g
I have 12 arrays named array1 through to array12 and i need to assign them to a global array called array that is passed in to the sorting algorithm
The 12 arrays are passed in to the array from a file
Having variables that look like array1, array2, array3,..., array12 is a sure sign that you need a single array instead of all these variables. You should put these arrays into an array of arrays, and use array[x] to access them.
For example, instead of
int[] array1 = new int[] {1, 2, 3};
int[] array2 = new int[] {4, 5, 6};
...
int[] array12 = new int[] {34, 35, 36};
you would write
int[][] array = new int[][] {
new int[] {1, 2, 3},
new int[] {4, 5, 6},
...
new int[] {34, 35, 36}
};
Now instead of writing array5 you would write array[4] (4, not 5, because indexes of Java arrays are zero-based). This indexing can be done with a for loop:
int[][] array = new int[][] { ... };
for (int i = 0 ; i != array.length ; i++) {
callMySort(array[i]);
}
or from a foreach loop:
int[][] array = new int[][] { ... };
for (int[] sortMe : array) {
callMySort(sortMe);
}

Accessing Arrays via an identifier variable - JAVA

I am trying to access an array based on a number. Let me explain:
stringBuilder.append(array1[i]);
but I have 4 arrays, I would like to access the array like this:
int i;
int aNum;
stringBuilder.append(array(aNum)[i]);
so the array number selected depends on the value of aNum (1 - 4) where [i] being the location of the array (0 - n)
This code however doesn't work. Any ideas? Tried looking on google but can't find the correct code I need. It does seem simple but can't find the solution. Hope it makes sense!
You are referring to a two-dimensional array, which is an array of arrays. Here is an example:
/**
<P>{#code java TwoDArray}</P>
**/
public class TwoDArray {
public static final void main(String[] ignored) {
//Setup
int[][] intArrArr = new int[4][];
intArrArr[0] = new int[] {1, 2, 3, 4};
intArrArr[1] = new int[] {5, 6, 7, 8};
intArrArr[2] = new int[] {9, 10, 11, 12};
intArrArr[3] = new int[] {13, 14, 15, 16};
StringBuilder stringBuilder = new StringBuilder();
//Go
int indexOfIntInSelectedArray = 1; //The second element...
int indexOfArray = 2; //...in the third array.
stringBuilder.append(intArrArr[indexOfArray][indexOfIntInSelectedArray]);
//Output
System.out.println("stringBuilder.toString()=" + stringBuilder.toString());
}
}
Output:
[C:\java_code\]java TwoDArray
stringBuilder.toString()=10
Arrays can contain theoretically contain any number of dimensions:
https://www.google.com/search?q=multi+dimensional+array+java
https://www.google.com/search?q=three+dimensional+array+java
https://www.google.com/search?q=four+dimensional+array+java
Your array is a two dimensional array (array of arrays). You need to index using two pairs of brackets:
int rows = 4;
int cols = 5;
int[][] myArray = new int[rows][cols];
int row;
int col;
stringBuilder.append(myArray[row][col]);

Categories