Int[] to Matrix Java - java

How do I convert a int[] to a matrix?
Let's say I want to make 6x6 matrix and I have an int[] array with 36 elements.
So the 00 entry is the 0th element of the array, 01: 1 element, 02:, 2nd element and so forth.

how about:
int[] ints = new int[36];
// fill with values
int[][] matrix = new int[6][6];
for (int i = 0; i < ints.length; i++) {
matrix[i / 6][i % 6] = ints[i];
}
To help you understand why this works, add the following inside the loop:
System.out.println("i = " + i + "; i / 6 = " + (i / 6) + "; i % 6 = " + (i % 6) + ";");

for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++)
matrix[i][j] = vector[i * 6 + j];

Related

Java- Moving elements from a ArrayList to an Array

I currently have a randomly mixed ArrayList.
public static void main(String[] args) {
ArrayList<Integer> solution = new ArrayList<>();
for (int i = 1; i <= 48; i++) {
solution.add(i);
}
Collections.shuffle(solution);
This gives me a ArrayList with the numbers 1-48 randomly mixed. Now I have 4 arrays and I want to randomly add the elements of the ArrayList with out repetition.
int[] heartsRow = new int[14];
int[] diamondsRow = new int[14];
int[] spadesRow = new int[14];
int[] clubsRow = new int[14];
The reason the new arrays contain 14 elements is because the first two elements will always be the same.
heartsRow[0] = 1;
heartsRow[1] = 0;
diamondsRow[0] = 14;
diamondsRow[1] = 0;
spadesRow[0] = 27;
spadesRow[1] =0;
clubsRow[0] = 40;
clubsRow[1] = 0;
I want to completely fill each array with non-repeating elements of the ArrayList.
You can make 4 for loops, from 0 to 11, 12 to 23, 24 to 35 and 36 to 47, and add in your lists.
for (int i = 0; i < 12; i++)
heartsRow[i + 2] = solution.get(i);
for (int i = 0; i < 12; i++)
diamondsRow[i + 2] = solution.get(i + 12);
for (int i = 0; i < 12; i++)
spadesRow[i + 2] = solution.get(i + 24);
for (int i = 0; i < 12; i++)
clubsRow[i + 2] = solution.get(i + 36);
You could use a counting loop over the list,
increment the counter by 4 in each step,
and assign elements to the arrays with adjusted offsets:
for (int i = 0; i + 3 < solution.size(); i += 4) {
int j = i / 4;
heartsRow[2 + j] = solution.get(i);
diamondsRow[2 + j] = solution.get(i + 1);
spadesRow[2 + j] = solution.get(i + 2);
clubsRow[2 + j] = solution.get(i + 3);
}

How to multiply the elements of a column in a 2D array in Java?

i have a 2D array 3X5 and i need the to multiply each element in column one, and so forth. This is what ive attempted without any luck. the result is not correct. i tried storing each column into an array and multiply each element from that array but i get the same results.
edit: yes i am aware there is no multiplication in this code, that is because it yields an incorrect product.
for(int j = 0; j < 5; j++){
double v = 0.0;
double[] ex = new double[3];
double volumeBox1 = 0.0;
for(int i = 0; i < 3; i++){
v = d[i][j];
System.out.println(v);
for(int z = 0; z < 3; z++){
ex[z] = v;
}
}
System.out.println("The volume of box " + (j+1) + " is: " + volumeBox1);
I will assume your matrix is 5 x 3, which is more logical and convenient than 3 x 5 for this use case :
for (int i = 0 ; i < d.length ; j++) {
double vol = 1;
for (int j = 0 ; j < d[i].length ; j++) {
vol *= d[i][j];
}
System.out.println("The volume of box " + (j + 1) + " is: " + vol);
}
This can of course be done with a 3 x 5 matrix but I think it makes less sense to iterate on the columns :
for (int j = 0 ; j < d[0].length ; j++) {
double vol = 1;
for (int i = 0 ; i < d.length ; i++) {
vol *= d[i][j];
}
System.out.println("The volume of box " + (j + 1) + " is: " + vol);
}

how to add two 1-d array and arrange it using for loop?

The user should input 10 integer values for list1 and 10 integer values for list2. Your program should add the contents of list1 and list2 then store the sum to list3. Your program should display horizontally the values of list1, list2, and list3. Use loops.
here is the output:
List1 : 1 3 2 5 7 8 5 6 9 4
List2 : 2 1 4 3 2 1 4 2 0 2
List3 : 3 4 6 8 9 9 9 8 9 6
here is my code
import java.io.*;
public class List {
public static void main(String[] args) {
int list1[] = new int[10];
int list2[] = new int[10];
int i, j, k, num = 0, num1 = 0, num2 = 0;
String input = " ";
BufferedReader in = new BufferedReader(new
InputStreamReader(System. in ));
for (i = 0; i < 10; i++) {
list1[i] = 0;
}
for (j = 0; j < 10; j++) {
list2[j] = 0;
}
try {
for (i = 0; i < 10; i++) {
System.out.print("Input value for list[" + i + "] = ");
input = in .readLine();
num = Integer.parseInt(input);
list1[i] = num;
}
for (j = 0; j < 10; j++) {
System.out.print("Input value for list[" + j + "] = ");
input = in .readLine();
num1 = Integer.parseInt(input);
list2[j] = num1;
}
} catch (IOException e) {}
System.out.print("list1: ");
for (i = 0; i < 10; i++) {
System.out.print(list1[i] + "\t");
}
System.out.println();
System.out.print("list2: ");
for (j = 0; j < 10; j++) {
System.out.print(list2[j] + "\t");
}
System.out.println();
int list3[] = new int[10];
list3[0] = list1[0] + list2[0];
list3[1] = list1[1] + list2[1];
list3[2] = list1[2] + list2[2];
list3[3] = list1[3] + list2[3];
list3[4] = list1[4] + list2[4];
list3[5] = list1[5] + list2[5];
list3[6] = list1[6] + list2[6];
list3[7] = list1[7] + list2[7];
list3[8] = list1[8] + list2[8];
list3[9] = list1[9] + list2[9];
System.out.print("list3: ");
for (k = 0; k < 10; k++) {
System.out.print(list3[k] + "\t");
}
}
}
my program is working correctly but i need to put the third list in loop can you help me?
In place of using sequential addition like in your code...
list3[0] = list1[0] + list2[0];
list3[1] = list1[1] + list2[1];
list3[2] = list1[2] + list2[2];
list3[3] = list1[3] + list2[3];
list3[4] = list1[4] + list2[4];
list3[5] = list1[5] + list2[5];
list3[6] = list1[6] + list2[6];
list3[7] = list1[7] + list2[7];
list3[8] = list1[8] + list2[8];
list3[9] = list1[9] + list2[9];
use this for() loop
for (int l = 0; l < list3.length; l++) {
list3[l] = list1[l] + list2[l];
}
Your first two for loops that take in values are fine, now what you will need is a for loop that does something like this:
for(int i = 0, int length = list1.lengthl; i < length; i++)
{
//add the stuff indexes by doing list1[i]+list2[i]
//make sure to assign it to a new array
}
Edit: If wondering why I use a variable to store the length, it is because a regular loop is usually O(n) [big-oh] this means the time it takes to finish is directly proportional to how large the loop is (aka, 100 iterations will take more than 10, it has a slope of one). However, if we were to do i

Convert 1d array to 2d array in JAVA

I am trying to input values from a 1d array into a 2d array in java.
This is what I have so far:
int[] input2 = {
0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0
};
int[][] arr = new arr[3][4];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.println("index" + ((i * arr.length) + j));
arr[i][j] = input2[(i * arr.length) + j];
//System.out.print(" " + arr[i][j]);
}
//System.out.println();
}
But what it outputs is:
index0
index1
index2
index3
index3
index4
index5
index6
index6
index7
index8
index9
which means that I am getting the indexes wrong from 1d array.
Where did I go wrong ?
Your mistake is that in each step you multiply by the number of rows rather than the number of columns.
If you want to get to the first element of the second row, you have to skip all the elements of the first row first. That would be 1 * arr[0].length. So your method may work in an X by X array, but not in an X by Y array.
It could well be filling in 2 dimensions, but ((i*arr.length) + j) is probably just adding the values. I would add '+ ", "' in the middle of it to see what is actually getting output.
Try this in your loop -
((i*arr[0].length) + j)
You should change your index formula from :
(i*arr.length) + j
to
(i*arr[i].length) + j
you can try this:
int count = 0;
for(int i=0; i< arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
//System.out.println("index" + ((i*arr.length) + j) );
arr[i][j] = input2[count++];
System.out.print(" " + arr[i][j]);
}
System.out.println();
}
Try
int[] input2 = {0,1,0,0,0,1,1,0,1,0,1,0};
int[][] arr = new arr[3][4];
for(int i=0; i< arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
System.out.println("index" + ((i*arr[i].length) + j) );
arr[i][j] = input2[(i*(arr[i].length)) + j];
//System.out.print(" " + arr[i][j]);
}
//System.out.println();
}
(Replaced ((i*arr.length) + j) with ((i*arr[i].length) + j)

How to add each element into a queue

How do I add each element into an arrayqueue? Basically, if I have an array of queues where each index is an arrayqueue that holds the 1s, 10s, 100s, etc. place of a corresponding 6 digit number in another index of array a. For example, if a[1] is 123456 then how can I make the code below hold arr[1] 654321? I've posted a question similar to this before but I'm just trying to get this right.
public static void radixSort(int[] a) {
//Create an array of 10 empty array queues
Queue[] arr = new Queue[a.length];
for (int i = 0; i < arr.length; i++)
arr[i] = new ArrayQueue();
for (int place = 1; place <= 100000; place *= 10) {
for (int i = 0; i < a.length; i++) {
arr[i].add(selectDigit(a[i],place));
// System.out.println("i: " + i + " a[i]: " + a[i] + " place: " + place + " digit: " + selectDigit(a[i],place));
}
}
// for (int i = 0; i < arr.length; i++)
// System.out.print(arr[i].remove()+ " ");
//for (int j = 0; j < arr.length; j++)
// a[j] = (Integer) arr[j].remove();
}
This tutorial might help:
http://www.sourcecodesworld.com/articles/java/java-data-structures/Radix_sort.asp
It seems a good walk through.

Categories

Resources