Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
While i was writing code for an accounting application , i observed unusual behavior of List in java. After executing below code,
List<String> accountsList = new ArrayList<String>();
for(int i=0; i< (Integer.MAX_VALUE+2) ;i++){
accountsList.add("Account #"+i);
}
System.out.println("# of accounts in list : "+accountsList.size());
got output as - # of accounts in list : 0 , which was very interesting. Also code ran correctly without throwing any exception. If this is because of value overflow of int, why did not java throw warning/ exception.
Then i modified condition in for loop as, for(int i=0; i< Integer.MAX_VALUE ;i++) and code worked as expected.
Is this behavior has to do anything with Max value of int, as ArrayList can hold values till count of Integer.MAX_VALUE ( accountsList.size() returns value of type int, and int has max value defined).
You try this one
System.out.println((Integer.MAX_VALUE+2));
The output is -2147483647 that is less than 0 so no values will be added to your list.
When you try to go above MAX_VALUE, you'll get a large negative number due to overflow. Technically, there is nothing illegal about this code and the compiler/java will not throw a warning/exception before or at run time.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I get how to count the number of words in a string by extinguishing the spaces between words, but am confused of how to approach it when it asks for strings equal to or exceed a certain number of characters.
Complete the countLongWords() method below, which takes an
array of Strings as its argument. This method returns an integer value
representing the number of Strings in the array that are 5 or more characters
long.
pseudocode
int count = 0
for str in stringArray
if str.length() >=5
count++
return count
You can convert this to java
Try to always think what you will do if you have a paper with this array of Strings written on it and want to count the number of Strings that satisfied this property.
Then it's very easy to translate that in a computer program.
Create a variable to hold the number of Strings with a length superior than 5
Loop through the element of your array
For each element, if it holds the property increment this variable
Return the variable
int number = 0;
for(int i = 0; i<array.length; i++){
if(array[i].length()>4){
number++;
}
}
return number;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to crate a loop which sums the arrays values which are within the interval (for instance 2-5). My main problem is getting from the first checked value of the array to the next one and so on. Thankyou in advance.
int x=0,y=0,s=0;
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();//lowest value
int c = scan.nextInt(); //highest value
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
//then check next one
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
You need to put these in a nested for-loop. Otherwise, they are executed only once, with x=0 and y=0. So all you are doing is essentially:
if (myArray[0][0]>b || myArray[0][0]<c)
s=s+myArray[0][0]
So s could only possibly be 0 or the first element in the 2D array.
Now, this conditional:
if (myArray[x][y]>b || myArray[x][y]<c)
does not mean what you think it means. This would evaluate to true for all numbers as long as b is less than c. For the semantics you are looking for, you want an AND operator (&&).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
int i=0;
while(!a.isEmpty()) {
if(a.toCharArray()[i]==' ') {
System.out.println(a.toCharArray()[i]);
}
i++;
}
When I run this program it's giving me an error: index out of bounds. How can I solve it?
You're not changing a in the loop, so a.isEmpty() will not change and you'll continue looping until i goes out of bounds. Did you mean:
while (i < a.length()) {
And as an aside, a.toCharArray()[i] can (and should) simply be a.charAt(i) (as #MartijnCourteaux also points out).
There are already a couple of answers that give solutions to the problem. This answer is about how you could have found the problem yourself.
Most array index out of bounds errors in loops are due to incorrect loop condition, so that the condition goes on being true even after the index has reached the end of the loop. Your loop condition did not depend on i, so the only way it could change from true to false is if a were changing inside the loop.
If you expected a to change, you should have put System.out.println(a) inside the loop, and monitored the value of a. You would have seen it not change.
Alternatively, if the isEmpty test were a typo, and you meant to test something that depends on i, desk checking your loop condition should have found it.
You should change your loop to this instead:
char[] c=a.toCharArray();
for(i=0; i<c.length; i++){
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do you make an array that has 1-3 int, instead of the default 0-3 int?
like
int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
You make an array of four elements, and then never use the '0' element.
Sorry, but you can't change the language.
The index starts at 0. You'll just need to set The variable different in your loops if it helps readability
Instead of for I=0; i
You'd use
For I= 1; I
Would recommend you get use to indexing from zero though.
Apologies for typos. Using phone
You don't, arrays in Java start their indices at 0.
And new int[3] does not have numbers 0-3, it has indices 0-2.
Java doesn't allow this. In most programming languages, array indexes start from zero. And in the cases where they start from one, you can't change the lower bound.
The only main-stream1 language I can think of where you could specify both the lower and upper bounds of an array is Ada. (And recent versions of Fortran too ...)
1 ... and calling Ada mainstream these days is "a stretch".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.