Add value to first index of value 0 in Java array [closed] - java

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 5 years ago.
Improve this question
If I have an array of zeros [0,0,0] how do I insert a given value to the first index that is valued 0.
So first off the value 3 will go into index 0 giving me [3,0,0]
Then the second value 8 will go into index 1 giving me [3,8,0].
How do I go about this?

You can edit the values in an array like this
int[] values = new int[]{0, 0, 0};
values[0] = 3;
values[1] = 8;
or if you really want to target the first index that is 0
for(int i = 0 ; i < values.length ; i++) {
if(values[i]==0){ // do stuff; break; }
}

int[] values = new int[]{0, 0, 0};
replaceFirstZeroValue( 3, values);
replaceFirstZeroValue( 8, values);
public void replaceFirstZeroValue(int value, int[] array){
for(int i = 0; i < array.length; i++){
if(array[i] == 0){
array[i] = value;
break;
}
}
}

Related

transfer data from one dimensional array in two-dimensional array 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
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;
}
}

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

Insertion sort array 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 years ago.
Improve this question
So I have to write code for an insertion sort that will sort an array of random ints, the array is already set up and working okay and everything but my sort is not, heres what i have:
for(int i =1; i< numberSort.length-1;i++){
int temp = numberSort[i];
int j = i-1;
while((j >= 0) && (numberSort[j]>temp)){
numberSort[j+1] = numberSort[j];
j = j-1;
}
numberSort[j+1] = temp;
}
}
It seems to me that that should work, however it does not, it moves the numbers around from their original position but does not order them in ascending order. Thanks for any help you may be able to offer.
This code works for me:
public static void main(String[] args) {
int[] numberSort = {22,7,2, 5, 7, 1, 2, 9,33,55,12,1,0};
for (int i = 1; i < numberSort.length; i++) {
int temp = numberSort[i];
int j = i - 1;
while ((j >= 0) && (numberSort[j] > temp)) {
numberSort[j + 1] = numberSort[j];
j = j - 1;
}
numberSort[j + 1] = temp;
}
for (int i = 0; i < numberSort.length; i++) {
System.out.println(numberSort[i]);
}
}
Gives output:
0
1
1
2
2
5
7
7
9
12
22
33
55

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