from Kotlin to 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 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());
}
}
}
}

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();
}

Pros and Cons of declaring expression in java? "sum += i" instead of "sum = sum + i" [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 got this question in an interview that what's the impacts of declaring expression sum+=i; or sum = sum+i; inside the loop.
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum = sum + i; //Expression 1
sum += i; //Expression 2
}
In this example, there is no difference whatsoever other than what you consider easiest to read.

Traverse the array and each time you encounter and change the value 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 3 years ago.
Improve this question
if Take an array of size 10 and take user inputs.how can i Traverse the array and each time you encounter '6' or '7', replace them with 60 and 70 respectively in java??
Maybe you can try this,
int[] input = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < input.length; i++)
if(input[i]==6 || input[i]==7)
input[i]*=10;
//Printing
for(int i : input)
System.out.println(i);
Here is a simplistic approach. Loop through the array and check each positions value. If it's a 6 or 7 replace as expected.
public static void main(String[] args){
int[] array = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < array.length; i++){
if(array[i] == 6){
array[i] = 60;
}
else if(array[i] == 7){
array[i] = 70;
}
}
for(int i : array){
System.out.println(i);
}
}

How to change values's position in 2d array in 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 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.

Categories

Resources