How to change values's position in 2d array in java? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i have given these matrix in java
name class
satish BCA
Neeraj MCA
how to rotate this to:
class BCA MCA
name satish neeraj

You can use this function for the same:
public static String[][] transpose(String[][] m){
String[][] temp = new String[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
temp[j][i] = m[i][j];
return temp;
}
Hope it helps.

Related

How to replace zeros in the array, with elements of another array in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
int[] a = {1,2,3,0,0,0};
int[] b = {7,9,13};
//resultant array: a={1,2,3,7,9,13}
}
Iterate through array A and replace elements with value zero with elements from B.
int n = a.length();
int m = b.length();
int j = 0;
for(int i=0; i<n; i++){
if(a[i] == 0 && j<m){a[i]=b[j];j++;}
}

How to solve Java "Nested Loops" problem? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want help in solving this problem: I want after run the program get this output:
enter image description here
and this is code:
System.out.println("n\tn^2\tn^3\tn^4");
System.out.println("----------------");
for (int i=1; i<5;i++)
{
for (int j= 1; j<4; j++)
{
System.out.print(Math.pow(i,j)+"\t");
}
System.out.println();
}
}
I don’t know what is wrong with me
the Math.pow return double and you need to cast it to int and you need to change the start number one more because the break loop immediately after the for loop condition is false not after the block execute.
System.out.println("n\tn^2\tn^3\tn^4");
System.out.println("----------------");
for (int i=1; i<6;i++)
{
for (int j= 1; j<5; j++)
{
System.out.print(((int)Math.pow(i,j)+"\t"));
}
System.out.println();
}

from Kotlin to Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on the problem. Could you please help me out and show how the below piece of code look like in Java.
(0 until n).forEach { i ->
var itis = sc.nextInt() - 1
allData[itis]!!.add(i)
if (allData[itis]!!.size == k) {
(0 until k).forEach { j ->
result[j].addFirst(allData[itis]!!.poll())
}
}
}
Try this:
for (int i=0; i<n; i++){
int itis = sc.nextInt() - 1;
if(allData[itis] != null){
allData[itis].add(i);
if (allData[itis].size() == k) {
for(int j=0; j<k; j++){
result[j].addFirst(allData[itis].poll());
}
}
}
}

Finding longest array in a three-dimensional array and returning a one-dimensional array that contains the largest array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
A method public static int[] largestArray(int[][][]a)
returns a single dimensional array that contains the largest array in the 3D array
If you wish to get the first element which matches your condition, you can try this code.
for(int i = 0; i < arr.length; i++) {
int[][] arrI = arr[i];
for(int j = 0; j < arrI.length; j++) {
int[] arrJ = arr[j];
if(arrJ.length > saved.length) {
saved = arrJ;
}
}
}
Hope that helps.

why I got error "java.lang.ArrayIndexOutOfBoundsException" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Create an int array named a with 1000 elements, and give the first element the value 1, the second element the value 2, and so on.
int[ ] a = new int[1000];
for (int i = 0; i <= a.length; i = i + 1){
a[i] = i;
}
java.lang.ArrayIndexOutOfBoundsException
If your edit is what the code actually was it's just because this check:
i <= a.length
This is not good because array indexes in Java run from 0 to length - 1. This means if your array length is 1000, the last index is 999.
So it should be the way it was before the edit:
for (int i = 0; i < a.length; i = i + 1) {

Categories

Resources