I have a single array populated with some values that I receive from another method, and I want to populate a bidimensional array with values from the first, example:
int[] singleArray; // there's no values here to demonstrate,
// let's think that's populated
int[][] bidimArray = new int[80][80];
for (int i = 0; i < 80; i++) {
for (int j = 0; j < 80; j++) {
for (int x = 0; x < singleArray.length; x++) {
bidimArray[i][j] = singleArray[x];
}
}
}
I thought in the solution above, besides it seems very ugly solution, it only saves the last position of singleArray in bidimArray[][]. May anyone help me, please?
There is no need for the third for loop here. This is where you went wrong. The change to your code is to simply increment x for every value entered into the new 2D array and omitting the third for loop.
int[] singleArray;
int[][] bidimArray = new int[80][80];
int x = 0;
for (int i = 0; i < 80; i++) {
for (int j = 0; j < 80; j++) {
bidimArray[i][j] = singleArray[x];
x++;
}
}
You can also combine the two inner lines in the loop like this:
bidimArray[i][j] = singleArray[x++];
As pointed out in the comments, you should not hard code array sizes. For your approach, you will have to make sure that the singleArray contains at least 80*80 elements. If this is not given, you should make sure to check that constraint beforehand.
Circular populating of a 2d array 8x7 with values from a 1d array 6. It works the same with larger and smaller arrays regardless of size:
int[] arr1 = {1, 2, 3, 4, 5, 6};
int m = 8;
int n = 7;
int[][] arr2 = IntStream.range(0, m)
.mapToObj(i -> IntStream.range(0, n)
.map(j -> arr1[(j + i * n) % arr1.length])
.toArray())
.toArray(int[][]::new);
// output
Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
[1, 2, 3, 4, 5, 6, 1]
[2, 3, 4, 5, 6, 1, 2]
[3, 4, 5, 6, 1, 2, 3]
[4, 5, 6, 1, 2, 3, 4]
[5, 6, 1, 2, 3, 4, 5]
[6, 1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 1]
[2, 3, 4, 5, 6, 1, 2]
See also: Copying a 1d array to a 2d array
Related
I have an array like this one-
{1, 2, 3, 4, 5, 6}
I want to sort it in the order of multiples of 3 with remainders 0, 1 and 2. (the first group is multiples of 3, the second one is multiples of 3 with remainder 1 and the last one is multiples of 3 with remainder 2) and I want to preserve the order in which elements appear in the array.
The result should be -
{3, 6, 1, 4, 2, 5}
I have this code-
int current = 0;
int b = 0;
for (int i = 0; i < 3; i++) { //3 groups
for (int j = current; j < numbers.length; j++) {
if (numbers[j] % 3 == i) { //reminder should be 0,1 or 2
b = numbers[j];
numbers[j] = numbers[current];
numbers[current] = b;
current++;
}
}
}
But this code does not preserve the order in which elements appear in the array. The result I got is-
{3, 6, 1, 4, 5, 2}
But I want the result to be like {3, 6, 1, 4, 2, 5}. How can I achieve this?
Using stream and comparator
int[] array = {1, 2, 3, 4, 5, 6};
List<Integer> lst = Arrays.stream(array)
.boxed()
.sorted(Comparator.comparingInt(o -> o % 3))
.collect(Collectors.toList());
System.out.println(lst);
In your solution you are swapping the elements in place, which shuffles them from the initial order. That's why you don't have the same ordering at the end. I'm not sure if there is another way apart from having a second array to keep the sorted elements, while at the same time iterating over the original one like so:
public static void main(String[] args) {
int[] numbers = new int[]{1, 2, 3, 4, 5, 6};
int[] result = new int[numbers.length];
int b = 0;
int current = 0;
for (int i = 0; i < 3; i++) { //3 groups
for (int j = 0; j < numbers.length; j++) {
if (numbers[j] % 3 == i) { //reminder should be 0,1 or 2
result[current] = numbers[j];
current++;
}
}
}
System.out.println(Arrays.toString(result));
}
Output: [3, 6, 1, 4, 2, 5]
You can use an IntStream and a Comparator to sort the stream:
int[] arr = {1, 2, 3, 4, 5, 6};
int[] arrSorted = IntStream.of(arr).boxed()
.sorted(Comparator.comparingInt(i -> i % 3))
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(arrSorted));
Output:
[3, 6, 1, 4, 2, 5]
Note: From IntStream.of() javadoc:
Returns a sequential ordered stream whose elements are the specified
values.
I would create a new array of the same size and then place the elements in the correct order. For example like this:
int[] array = {1, 2, 3, 4, 5, 6};
int[] sorted = new int[array.length];
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < array.length; j++) {
if (array[j] % 3 == i) {
sorted[counter] = array[j];
counter++;
}
}
}
System.out.println(Arrays.toString(sorted));
Output:
[3, 6, 1, 4, 2, 5]
Alternatively, you can use Java 8 features to reduce the amount of code like this:
int[] array = {1, 2, 3, 4, 5, 6};
int[] sorted = Arrays.stream(array).boxed().sorted(Comparator.comparingInt(a -> (a % 3))).mapToInt(i -> i).toArray();
Output:
[3, 6, 1, 4, 2, 5]
I have two 2D arrays of integers of the same dimension that I need to compare. I am going to explain what I need with an example where Sjm1 is the "original" array and the Sjm2 array has the same values as Sjm1 but the values of each column are ordered in increasing order (i.e. "0" is the min value of Sjm1 in column 0 so Sjm2[0][0]=0; then "70" is the next min value of Sjm1 in column 0 ⇒ Sjm2[1][0]=70; and so on). I have a method to sort a "Sjm1" array to "Sjm2".
Now I need to build an "output" array (2D array of integers) where the values in each column represent the number of the row in Sjm1 array that coincides with the elements of the column in Sjm2. For example, Output[0][0]=5 because Sjm1[0][0]=366 is Sjm2[5][0]=366; Output[1][0]=2 because Sjm1[1][0]=104 is Sjm2[2][0]=104; and so on).
Thus, for the previously presented example, the needed output must be the following:
I tried to build a method in Java where I pass the two 2D arrays of integers but is not working as needed:
public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
int k;
int[][] output = new int[Sjm1.length][Sjm1[0].length];
for (int j = 0; j < Sjm1.length; j++) {
k = 0;
for (int m = 0; m < Sjm1[0].length; m++) {
if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
output[j][m] = k;
k++;
}
}
}
return output;
}
The output is clearly not what I need:
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
I'll be glad if someone can help me.
I think you are not updating the value of k correctly, if I understood what you need, once you find the value you are looking for just update the value of k to the index you found the value in. Note that if you have repeated values it will only take the first it found.
public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
int k;
int[][] output = new int[Sjm1.length][Sjm1[0].length];
for (int j = 0; j < Sjm1.length; j++) {
k = 0;
for (int m = 0; m < Sjm1[0].length; m++) {
if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
k = j;
output[j][m] = k;
break;
}
}
}
return output;
}
I'm kinda new to contributing, so please let me know if I got this wrong! ><"
The problem is with your code, you're comparing both of the matrices at the same intervals (as m and j changes). What you could do, is iterate through matrix sjm2 and compare for each iteration for matrix sjm1.
i.e.
Have value of SJM1
Iterate through that column in SJM2 to find the row with the same val
Add the row number in the output.
Plus, the only way you got your output is if your Sorted_Sjm2 is the same as your Sjm1, as k was incremented to 6 every iteration.
Inside your inner loop (which sets output[j][m]) an easy way to find the matching index would be to use List.indexOf rather than searching for it yourself:
output[j][m] = Arrays.asList(Sjm2[j]).indexOf(Sjm1[j][m]);
If I get your question right, the problem is with the k variable.
It represents the row index on the second array, so when you compare the value on the two array, you should index the row of the second array with k.
Also, you should iterate the k over all rows in the current column, so the code would look like this (I modified the variables to make them more describing):
public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
int[][] output = new int[Sjm1.length][Sjm1[0].length];
for (int row = 0; row < Sjm1.length; row++) {
for (int column = 0; column < Sjm1[0].length; column++) {
int valueToSearchFor = Sjm1[row][column];
for (int rowInSorted = 0; rowInSorted < Sorted_Sjm2.length; rowInSorted++) {
if (Sorted_Sjm2[rowInSorted][column] == valueToSearchFor) {
// Found
output[row][column] = rowInSorted;
break;
}
// Not found
output[row][column] = -1;
}
}
}
return output;
}
Note that although this code works, I doubt that it's optimal, so I would not use it for very large data sets.
At first you need to iterate on Sorted_Sjm2[j][] to find out where is the Sjm1[j][m] value so you need another for in for (int m = 0; m < Sjm1[0].length; m++). And the other thing is why you are using k?k doesn't show any thing in your array. If you want gain the sorted position you should use another variable that you declare in new for statement in
Sorted_Sjm2[j][]. Because we know you are in column j so we just need to now row number of the sorted array.
public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
int k;
int[][] output = new int[Sjm1.length][Sjm1[0].length];
for (int j = 0; j < Sjm1.length; j++) {
for (int m = 0; m < Sjm1[0].length; m++) {
for (int d = 0; m < Sjm1[0].length; d++) {
if (Sorted_Sjm2[j][d] == Sjm1[j][m]) {
output[j][m] = d;
}
}
}
}
return output;
}
To get a matrix of row indices by columns of elements of this matrix in a sorted matrix - you can first sort the row indices of the elements of this matrix by columns and get the sorted transposed matrix of indices. Then transpose the sorted matrix back and, for each element, swap its value and column index:
int m = 7;
int n = 8;
int[][] arr1 = new int[][]{
{366, 139, 223, 312, 563, 471, 437, 2},
{104, 195, 85, 0, 377, 289, 227, 5},
{451, 221, 329, 425, 523, 591, 537, 1},
{208, 78, 0, 140, 437, 380, 286, 6},
{0, 52, 114, 84, 296, 212, 205, 3},
{70, 0, 40, 121, 194, 156, 123, 3},
{299, 351, 446, 216, 648, 685, 571, 2}};
int[][] arr2 = IntStream
// iterate over the indices
// of the rows of the array
.range(0, n)
.mapToObj(i -> IntStream
// iterate over the
// indices of the columns
.range(0, m)
.boxed()
// sort indices of the elements of the
// columns by its values in the array
.sorted(Comparator.comparingInt(j -> arr1[j][i]))
.mapToInt(Integer::intValue)
// sorted column of indices
// is a row in the new array
.toArray())
// return sorted array of indices
.toArray(int[][]::new);
// transpose the array of indices
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i ->
IntStream.range(0, n).forEach(j -> {
// swap the column index and
// the value of the element
int val = arr2[j][i];
arr3[val][j] = i;
}));
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
Output:
[5, 3, 4, 5, 5, 4, 4, 1]
[2, 4, 2, 0, 2, 2, 2, 5]
[6, 5, 5, 6, 4, 5, 5, 0]
[3, 2, 0, 3, 3, 3, 3, 6]
[0, 1, 3, 1, 1, 1, 1, 3]
[1, 0, 1, 2, 0, 0, 0, 4]
[4, 6, 6, 4, 6, 6, 6, 2]
See also:
• Sorting 2d array of integers by column
• Finding the position of a row element in a 2d ordered array
How can I add the values in the arrNumbers that exceed 6 to add to a new array starting from the last value and ending at the first.
This is what I have written, but does not produce the right output.
int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2 };
int[] newArrNumbers = new int[6];
for(int i = 0; i < arrNumbers.length ; i++){
newArrNumbers[i % 6] += arrNumbers[i];
}
The actual output:
newArrNumbers = [2, 4, 3, 4, 5, 6]
However, I want the output to ADD to the LAST VALUE in the arrNumbers, going from right to left, not left to right. So result should be:
newArrNumbers = [1, 2, 3, 4, 7, 7]
Try this.
int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2 };
int[] newArrNumbers = new int[6];
for(int i = 0; i < arrNumbers.length ; i++){
newArrNumbers[i < 6 ? i : (6 - (i % 6) - 1)] += arrNumbers[i];
}
System.out.println(Arrays.toString(newArrNumbers));
output:
[1, 2, 3, 4, 7, 7]
For example, if I have an array a= {1, 2, 6, 10}, it should print all combinations of these 4 numbers, there should be 4! total combinations. An array of 5 integers will have a total of 5! combinations. (Different from previous versions because my number of parameters have to stay same. I am not allowed to put more than 3 paramrters in.
array a= {1, 2, 6, 10}
{1, 2, 10, 6}
{1, 6, 2, 10}
{1, 6, 10, 2}
.
.
.
{10, 6, 2, 1}
I am trying to solve this process using Recursion, any idea how? this is the code i have for now. Can anyone who has any idea whats going on please help me?
static void permutations (int a[], int n, int p){
if (p==n-1)
return ;
for (int i=p; i<n; i++){
int b[]=new int [n];
b[p]=a[i];
for (int j=0; j<i; j++)
b[j]=a[j];
for (int k =i+1; k<n; k++)
b[k]=a[k];
System.out.println(Arrays.toString(b));
return permutations(b, n, p+1);
}
}
best way to solve your problem to use Permutations concept in Algebra it's very useful way to get all possible combinations of a set.
you should also have knowledge in possibilities to solve this problem:
in this set {1, 2, 6, 10} element 1 has a k-Permutations which is 3
and this Permutations is : {2, 1, 6, 10},{6, 2, 1, 10},{10, 2, 6, 1}.
also element 2 has k-Permutations which is 2
2 Permutations is : {1, 6, 2, 10},{1, 10, 6, 2}.
element 6 has a k-Permutations which is 1
6 Permutations is : {1, 2, 10, 6}.
finally the total possible Permutations for all elements in set : 3 * 2 * 1 = 6.
simple algorithm to print all possible Permutations:
int[] num = {1,2,6,10};
int[] a = num.clone();
ArrayList<String> combs = new ArrayList<String>();
int temp;
for(int i=0; i < num.length-1; i++){
for(int j=i+1; j < num.length; j++){
temp = a[i];
a[i] = a[j];
a[j] = temp;
String obj = Arrays.toString(a);
if(!combs.contains(obj)){
combs.add(obj);
}
a = num.clone();
}
}
System.out.println("All possible order Combinations:");
for(String obj : combs){
System.out.println(obj);
}
output:
All possible order Combinations:
[2, 1, 6, 10]
[6, 2, 1, 10]
[10, 2, 6, 1]
[1, 6, 2, 10]
[1, 10, 6, 2]
[1, 2, 10, 6]
hope this would help.
I have had a problem when I am doing my java program. I want to generate all the possible combinations of the element in a String array, and store each possible one into an overall ArrayList.
I used a for loop to go through all the possible combinations and use ArrayList.add to add each String array. However, when I was trying to print out the ArrayList to check, it only have the last String array at all the positions.
If I move the System.out.println to the inside of for loop, everything looks just fine. If I move the print to the outsides the loop, it just show that I only have the same String array at all positions.
Problem related to two parts of code:
String[] inputs = {"1", "2", "3", "4", "5", "6"};
int maxLength = 4;
//Get the total number of all combinations with replacement, used for the for loop
int total = (int) Math.pow(inputs.length, maxLength);
ArrayList<String[]> allList = new ArrayList<>();
System.out.println(total);
String[] subSets = new String[maxLength];
int [] index = new int [maxLength];
Arrays.fill(index, 0);
for (int i = 0; i < total; i++)
{
for (int j = 0; j < maxLength; j++)
{
subSets[j] = inputs[index[j]];
}
allList.add(i, subSets);
if (i != (total - 1))
index = nextIndex (index, maxLength, inputs.length);
// Set the print here everything looks fine
System.out.println(Arrays.toString(allList.get(i)));
}
// However, if you pit it here to check if you get the correct ArrayList, problem happens
//for (int g = 0; g < total; g++)
//System.out.println(Arrays.toString(allList.get(g)));
Another part is:
// Get the index of the next possible combination
public static int[] nextIndex (int[] index, int maxLength, int siZe)
{
for (int i = (maxLength - 1); i > 0; i--)
{
if (index[i] == (siZe - 1))
{
index[i] = 0;
if(index[i-1] == (siZe - 1)){
continue;
}
index[i - 1]++;
break;
}else{
index[i]++;
break;
}
}
The idea of this part of my program is to generate all possible combinations (with replacement) from the String array "inputs" and store the combinations into an overall ArrayList. Printing them out just my habit to check whether each step is correct or not.
The wrong output that I keep getting (just part of the output):
[6, 6, 6, 6]
[6, 6, 6, 6]
[6, 6, 6, 6]
[6, 6, 6, 6]
[6, 6, 6, 6]
[6, 6, 6, 6]
The correct output that I want to get:
[1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 3]
[1, 1, 1, 4]
[1, 1, 1, 5]
[1, 1, 1, 6]
You are creating the subsets array just outside the for loops, so you are always updating the same array. this is the reason why, at the end you get the last permutation.
move the "String[] subSets = new String[maxLength];" just inside the for loop related to "i"
for (int i = 0; i < total; i++)
{
//create the new array here
String[] subSets = new String[maxLength];
...
}
and you will get the right output:
1296
[1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 3]
[1, 1, 1, 4]
[1, 1, 1, 5]
.....
[6, 6, 6, 4]
[6, 6, 6, 5]
[6, 6, 6, 6]
You are creating the subSets Array outside your for loop and hence everything in the list is referring to the last updated subSets object i.e. [6, 6, 6, 6]. You are adding the same object to your list again and again and hence it will update the latest values at all the places in your list.
Move it inside your for loop as follows:
for (int i = 0; i < total; i++)
{
/* Move It Here */
String[] subSets = new String[maxLength];
for (int j = 0; j < maxLength; j++)
{
subSets[j] = inputs[index[j]];
}
allList.add(subSets);
if (i != (total - 1))
index = nextIndex (index, maxLength, inputs.length);
}