transfer data from one dimensional array in two-dimensional array 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
Assume I have one dimensional array
int[] arr = new int[]{1,2,3,4,5,6)
and I need transfer all data from this arr in two-dimensional array
int[][]array = new int[2][1];
bellow code:
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[i];
}
}
return result:
[[1], [2]]
I need:
[[1,2,3], [4,5,6]]
How to achieve it?

Your 2d array is too small. To get the output you want, it needs to have a shape of 2 by 3.
int[][] array = new int[2][3];
You will also need to change the loop that builds the array. It has to keep track of the index for the original array separately from the indices of the 2d array.
int indexForArr = 0;
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[indexForArr];
indexForArr = indexForArr + 1;
}
}

Related

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.

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

I have a loop that displays 10 values: 1.0, 1.1,1.2, etc. how can I put those values into an 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 6 years ago.
Improve this question
I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:
int i = 0;
int x = 10;
for(i;i>x;i++)
{
System.out.println(x);
}
but instead of displaying the values, I want to put them in an array. How do I do that?
How about:
// You want x ints.
int x = 10;
// Make an array big enough to hold x ints.
int[] array = new int[x];
// Loop x times.
for(int i = 0; i < x; i++) {
// Put the next number into the array.
array[i] = i;
}
first your way in writing for loop is need to be more clean
it should :
for(int i=0; i > x; i++){
System.out.println(x);
}
second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.
third to put the values in array :
simply define array : int[] numbers = new int[size of array];
then put each value inside the index i of array :
numbers[i] = i;
finally for loop will be like:
for(int i=0; i < x; i++){
numbers[i] = i;
}

Printing indices of an 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 7 years ago.
Improve this question
I'm having trouble figuring out why my j won't print out with the current boolean statement. Its only if I put != will it then print 100,000-1 indices.
public static void main(String[] args) {
//array size and name
double[] largearr;
largearr = new double[100000];
double index = 0.00149;
int j = 0;
//population of array and the printout of the elements
for(int i = 0; i < 100000; i++) {
Arrays.fill(largearr, index);
index += 0.00149;
// System.out.println(largearr[i] + " ");
}
for(j = 0; j < largearr.length; j++) {
if(largearr[j] == 58.00122999995376) {
System.out.println("j");
}
}
}
Because you don't have that value in any position of your largerarr. The first for loop in your code fills all the elements of largearr array with the newly incremented index value, and it does it 100000 times: at the end you'll have largearr filled with the last iteration's value of index, that surely isn't 58.00122999995376. So the second for loop doesn't enter the if check for any value and the program quits without printing anything.
If you could explain what are you trying to achieve maybe we could help you better.

convert enhanced for loop to for loop [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 years ago.
Improve this question
how to convert this 'enhanced for loop' below to 'for loop'
ArrayList<Integer> input= new ArrayList<Integer>();
int[] copies = new int[20];
for(int allCopies : input) {
copies[allCopies]++;
}
we've tried this:
for(int k = 0; k < input.size(); k++) {
for this part:
for(int allCopies : input) {
but we don't know how to get this part:
copies[allCopies]++;
any HELP?????
This should do it:
int allCopies = input.get(k);
copies[input.get(k)]++;
You should look at the API first to find the appropriate method to use.
ArrayList<Integer> input= new ArrayList<Integer>();
int[] copies = new int[20];
for(int k = 0; k < input.size(); k++) {
copies[input.get(k)]++;
}
In an enhanced for loop for(int allCopies : input)
allCopies is the same as input[counterPosition].
So basically all this is doing is getting that value using k as the counter.
input.get(k)

Categories

Resources