I have an integer array.
int[] arr1 = new int[] {3, 4, 5};. I want to insert this array when initializing another array. So that when
int[] arr2 = new int[] {8, 7, arr1, 1, 0};, arr2 will equal {8, 7, 3, 4, 5, 1, 0}.
Also, I do not know the length of arr1, so I can't do int[] arr2 = new int[] {8, 7, arr1[0], arr1[1], arr1[2], 1, 0};
Any help is greatly appreciated.
Sadly as far as I'm aware something like that can't be done in a single operation. The best I was able to come up with is to create a result array. Then you copy to it first part of arr2, whole arr1 and finally second part of arr2. In the end whole method would look like so:
private static int[] insertArrayAtPosition(int[] arr1, int[] arr2, int insertPos){
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr2, 0, result, 0, insertPos);
System.arraycopy(arr1, 0, result, insertPos, arr1.length);
System.arraycopy(arr2, insertPos, result, insertPos + arr1.length, arr2.length - insertPos);
return result;
}
And then you can call it like so:
public static void main(String args[]) {
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[] {8, 7, 1, 0};
int[] result = insertArrayAtPosition(arr1, arr2, 2);
System.out.println(Arrays.toString(result));
}
Edit: Milgo's solution looks much better. However, mine allows to insert one array into another at specific position after they are already created. Which one to use depeds on the usecase.
You could use ArrayUtils.addAll() three times.
int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[arr1.length + 4];
ArrayUtils.addAll(arr2, new int[]{8, 7});
ArrayUtils.addAll(arr2, arr1);
ArrayUtils.addAll(arr2, new int[]{1, 0});
ArrayUtils is part of Apache Commons.
This is not natively supported in java / there is no language feature that allows you to use a one liner.
You can of course write a loop that inserts the elements of the other array, you can also use list interfaces or Arrays#stream. The best way to do it should probably be to use lists, like this:
List<Integer> arr1 = Arrays.asList(3, 4, 5);
List<Integer> arr2 = Arrays.asList(8, 7, 1, 0);
arr2.addAll(2, arr1);
Hope this helpes.
Edit: It is a one liner btw :)
Related
public class Main {
public static void main(String[] args) {
int[][] numbers = {{1, 2, 3} , {4, 5, 6, 7}}; //Is it defined?
int x = numbers[0][3]; // Should the output be 0?
System.out.println(x);
}
}
you may take a look here into the official oracle specification:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
When you take a look on chapter (array access) you will see that:
"All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1."
A 2D array is really an array of arrays. So you can do this.
int[][] arr = new int[4][];
arr[0] = new int[] {1,2,3};
arr[1] = new int[] {4,5,6,7};
arr[2] = new int[] {8,9,10,11,12,13};
arr[3] = new int[] {14,15};
And to print them.
for (int[] a : arr) {
System.out.println(Arrays.toString(a));
}
Prints
[1, 2, 3]
[4, 5, 6, 7]
[8, 9, 10, 11, 12, 13]
[14, 15]
Technically, there are no two-dimensional arrays. What you can have is an array of arrays; this is conveyed precisely by the [][] syntax.
In your example,
int[][] numbers = {{1, 2, 3} , {4, 5, 6, 7}};
the 0'th element of numbers, i.e., numbers[0], is a 3-element array, whereas numbers[1], is a 4-element array.
Accessing numbers[0][3] will raise an exception, since 3 is out of bounds for the 3-element array.
Reference to Java Language Specification: Chapter 10, Arrays.
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);
}
Say I have less than ten two dimensional arrays representing matrices in java. Each array is enumerated from 0 to 6. Each array is n*2^enumval larger where n is the number of elements from the 0th enumerated array, thus my multidimensional will almost be pyramid shaped, hence the need for a jagged three dimensional array. Is it possible to create a jagged three dimensional array?
int[][] array0 = new int[2][2];
int[][] array1 = new int[4][4];
int[][] array2 = new int[8][8];
int[][] array3 = new int[16][16];
int[][][] arrayStorage = new int[][][];
I would like to to store the arrays with the following approach: arrayStorage[columnOfArray#][rowOfArray#][#]
Yes, it's perfectly possible to create a jagged array in Java.
Here's some sample syntax for it:
int[][] jagged = new int[3][];
jagged[0] = new int[]{1, 2, 4};
jagged[1] = new int[]{9, 17, 35, 80};
jagged[2] = new int[]{100};
In this, I have a jagged array with three rows but an indeterminate amount of columsn. THeir lengths vary from 1 to 4.
Java only supports jagged arrays. An example of the sort of 3D array (using a "list" of "matrices" as a more relatable concrete example) you're looking for:
// an array of m*n (or jagged) 2D arrays.
int[][][] matrices = new int[2][][];
// A 2*2 matrix
int[][] matrix22 = new int[2][];
matrix22[0] = new int[] {1, 2};
matrix22[1] = new int[] {3, 4};
matrices[0] = matrix22;
// A 3*3 matrix
int[][] matrix33 = new int[3][];
matrix33[0] = new int[] {1, 2, 3};
matrix33[1] = new int[] {4, 5, 6};
matrix33[2] = new int[] {7, 8, 9};
matrices[1] = matrix33;
Or, golfed to one statement:
int[][][] matrices2 = new int[][][] {
new int[][] {
new int[] {1, 2},
new int[] {3, 4}
},
new int[][] {
new int[] {1, 2, 3},
new int[] {4, 5, 6},
new int[] {7, 8, 9}
}
};
Let's say I have an array:
int array[][] = {{1, 2, 3}, {2, 5, 7}, {4, 2, 1}};
How can I randomly make it
int array[][] = {{2, 5, 7}, {1, 2, 3}, {4, 2, 1}};
or
int array[][] = {{4, 2, 1}, {2, 5, 7}, {1, 2, 3},};
And so on.
Is there any JAVA function to help me? Or I have to figure it out by myself?
Thank you.
You might convert your array to a List<int[]> and call Collections.shuffle(). Then convert back to an array.
int array[][] = {{1, 2, 3}, {2, 5, 7}, {4, 2, 1}};
List<int[]> l = Arrays.asList( array ); //the list returned is backed by the array, and thus the array is shuffled in place
Collections.shuffle( l );
//no need to convert back
If you need to keep the original order, you'd have to create a copy of the array (or the list backed by that array), like this:
int array[][] = {{1, 2, 3}, {2, 5, 7}, {4, 2, 1}};
List<int[]> l = new ArrayList<int[]>( Arrays.asList( array ) ); //creates an independent copy of the list
Collections.shuffle( l );
int newArray[][] = l.toArray( new int[0][0] );
Another way:
int array[][] = {{1, 2, 3}, {2, 5, 7}, {4, 2, 1}};
int newArray[][] = array.clone(); //copy the array direcly
List<int[]> l = Arrays.asList( newArray );
Collections.shuffle( l );
It is very simple using the Collections.shuffle(..) method as the Arrays.asList(..) method returns a List backed by the array.
Collections.shuffle(Arrays.asList(array));
Full example:
public static void main(String... args) {
int array[][] = {{1, 2, 3}, {2, 5, 7}, {4, 2, 1}};
Collections.shuffle(Arrays.asList(array));
for (int[] a : array)
System.out.println(Arrays.toString(a));
}
If you can use collections there is a shuffle method, if you have to use a primitive type such as int, you will have to shuffle it yourself. Here are examples of both:
http://blog.ryanrampersad.com/2008/10/13/shuffle-an-array-in-java/
Try the Collections class that comes with Java. You can use the shuffle() method to randomize the indices to access the arrays.
Link to Java API
Use Collections:
something like this:
List<int[]> list = Arrays.asList(array);
Collections.shuffle(list);
int[][] shuffledArray = (int[][]) shuffledList.toArray();
What you want is random swaps of the contents of the outer array.
You could use java.Random's nextBoolean() to get a true/false as to whether to make a swap, such as between 1&2 or 1&3 or 2&3.
This is assuming you want to work with the primitive types, and not classes.
How can I store arrays in single array?
e.g. I have four different arrays, I want to store it in single array int storeAllArray [] and when I call e.g. storeAllArray[1] , I will get this output [11,65,4,3,2,9,7]instead of single elements?
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int storeAllArray [] = {array1,array2,array3,array2} // I want store all array in on array
for (int i=0; i<storeAllArray; i++){
System.out.println(storeAllArray.get[0]); // e.g. will produce --> 1,2,3,4,5,100,200,400 , how can I do this?
}
EDITED:
How can I get output like this?
System.out.println(storeAllArray [0]) --> [1,2,3,4,5,100,200,400];
System.out.println(storeAllArray [1]) --> [2,6,5,7,2,5,10];
System.out.println(storeAllArray [2]) --> [11,65,4,3,2,9,7];
System.out.println(storeAllArray [2]) --> [111,33,22,55,77];
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int[] storeAllArray [] = {array1,array2,array3,array4};
for (int[] array : storeAllArray) {
System.out.println(Arrays.toString(array));
}
In Java 5 and above, this prints
[1, 2, 3, 4, 5, 100, 200, 400]
[2, 6, 5, 7, 2, 5, 10]
[11, 65, 4, 3, 2, 9, 7]
[111, 33, 22, 55, 77]
Prior to Java 5, you should use
System.out.println(Arrays.asList(array));
If I understand your question, you want to "flatten" those arrays to one array. Look at rosettacode.org for such example in Java and other languages.
int array1[] = { 1, 2, 3, 4, 5, 100, 200, 400 };
int array2[] = { 2, 6, 5, 7, 2, 5, 10 };
int array3[] = { 11, 65, 4, 3, 2, 9, 7 };
int array4[] = { 111, 33, 22, 55, 77 };
int[][] storeAllArray = new int[][] { array1, array2, array3, array2 };
for (int j : storeAllArray[0]) {
System.out.print(j + ", ");
}
To access a single element of the selected array you need to do something like:
storeAllArray[i][j]
use the following syntax
int[][] storeAllArray = {array1, array2, array3, array4};
Create array-of-arrays:
int[] array1 = {1,2,3,4,5,100,200,400};
int[] array2 = {2,6,5,7,2,5,10};
int[] array3 = {11,65,4,3,2,9,7};
int[] array4 = {111,33,22,55,77};
int[][] allArrays = {
array1, array2, array3, array4
};
System.out.println(java.util.Arrays.toString(allArrays[0]));
// prints "[1, 2, 3, 4, 5, 100, 200, 400]"
There is no need to do so.
You can use a two dimensional array for the job.
Make sure the row length should be equal to the array with max length.
int a[]={1,2,3,4,5,6};
int b[]={4,8,3,6,4,5};
int c[][]=new int[2][6];//here 2 refers to the no. of arrays and 6 refers to number of elements in each array