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) {
Related
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.
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 2 years ago.
Improve this question
Example:
Input:
140
Output:
1 4 0
I wanted to make it divide by 100 so that the outcome will be 1, and then divide by 10 and the outcome will be 4 and then by 1 and the answer will be 0. But I am not sure how I am able to achieve it. I also want to use recursion in the method.
You can see an int as a String
int n = 140;
String s = String.valueOf(n);
for(int i = 0; i<s.length(); i++){
System.out.println(s.charAt(i));
}
With no need of recursion.
With recursion it could be something like (i haven't tried it so it could not work):
public String separateInteger(int n){
if(n < 10){
return String.valueOf(n);
}
else{
int mod = n%10;
int quot = n/10;
return String.valueOf(mod) + separateInteger(quot);
}
}
I hope have answered your question. :)
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.
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.
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
I have a for loop that prints a string multiple times.
I need to know:
How do I get the number of times the string is printed?
Thanks!
int count = 0
for (int i = 0 ; i < stop_criteria; i++){
// print string
count++;
}
printf("Number of times printed %d", count);
or as TheLostMind suggest:
int i;
for (i = 0 ; i < stop_criteria; i++){
// print string
}
printf("Number of times printed %d", i);