Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to sum the elements of 2 different lists.
in my test list 1 contains 1 and 2
and list 2 contains 3 and 4.
I want to sum the elements of list 1 and list 2 like this:
1+3, 1+4, 2+3, 2+4.
I've tried the code below,
but it does not work.
my code:
for (int i = 0; i < l1.size(); i++) {
for (int j = 0; j < l2.size(); j++) {
System.out.println( l1.get(i) + l2.get(i) );
}
}
my output always shows
5
5
5
5
kindly help and correct me if im wrong or missed the logic.
you have to use j at some point, at which you should be able to figure out yourself
this is a very common error with starters I guess, because using "i" becomes a unquestioned habit
to avoid this problem you could start using another naming pattern,
eg. call them it1 and it2 when iterating lists, and x y when traversing coordinates etc., this way you know what you intended to do with them
this also can improve readability a lot,
if you ever have to refine a complex nested for you will curse at not using better names
you might want to consider using the refactoring (in eclipse strg+1 and "rename in file") to give the iterators a more meaningful name afterwards, or if you get confused midway through the algorithm even beforehand
Related
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
I need to find a way to implement an algorithm in Java that can calculate (a+b+...+n)^2 with a,b,n >= 0. The purpose is to use it afterwards in order to calculate Jain's Fairness index for my algorithm in networks. Is there any standard way to do that or any specific library for advanced math that i might have missed?
Just sum those n values in for loop and then multiply it by itself. Or am i missing something?
The number of possible problems you may encounter is infinite, so you should not be surprised if you often get into a situation where there is no method to help you. Let's suppose that you have an array of numbers, let's suppose it has double elements and the name of the array is input, then:
double sum = 0;
for (int index = 0; index < input.length; index++)
sum += input[index];
double result = Math.pow(sum, 2);
//output holds the result
If there is a possibility of overflow, then you will need to handle it. Also, you will need to handle the validation that your items are positive.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am new to Java programming and I am unable to check whether for some i < length(of array) , whether arr[i] is valid or NULL .
Lets define a class student that has instance variables as name and roll number.
If the array of Students has student object stored at positions 1 , 3 and 5 , How do I , using a for loop from i = 0 to i = 5 print the name and roll numbers without having an NZEC (knowing that arr[0] , arr[2] and arr[4] does not have anything stored) .
I know that in c++ , we can directly check using :
if (arr[i] != NULL)
print(name,roll)
But , how do I do it in Java ?
Remember, Java is a case sensitive programming language and the null keyword is lowercase. Thus comparing if(arr[i] != NULL) would give you a syntax error, you want to type if(arr[i] != null):
for(int i = 0; i < arr.length; i++){
if(arr[i] != null){
System.out.println(arr[i]);
}
}
These kind of errors and mistakes can be easily avoided by using an IDE. IDEs usually find errors in your code before you compile it, and will point them out so you can fix them. Most IDEs will provide a possible solution to your problem in a addition to finding it, so you can't go wrong. I suggest you use Eclipse IDE for Java Developers.
You can try:
Object[] arr = {obj1,null,obj2,obj3};
for (int i=0;i<arr.length;i++) {
if(arr[i]!=null) {
//Do what you want, obj is not null
} else {
//Do what you want, obj is NULL!
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
enter image description here
This is the code screenshot link.
I am edit many time but every time the output is not completely sorted.
Your problem is very simply: you are increasing k far too often within your code!
Meaning: you are already looping with k; so you got:
for (int k=0; k < a.length; k++) { // dont use hardcoded "9" here btw!
and then you have
k++ again in the loop body. You simply dont have to do that!
Meaning: your k is growing faster than it should. But as your loop stops when k reaches 9; you are not processing all elements in your array!
Plus: insertion sort doesn't work with iterating your array once!
You have to keep iterating until all elements are in their place! You really want to study/think more about this algorithm. You, not us that is!
And as said, dont use hard-coded limits for arrays. You already say once that your array should contain 10 elements. From there on, you should be using a.length only! Besides: only use one-letter names for loop counters and such things. "a" is a pretty bad name for an array; why dont you call it "numbers" for example.
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
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There is an issue with for the '[ right in front of the data-testing-id and I am not sure why. Any help is appreciated.
for (int i = 1; i < 1001; i = i + 1) {
if(driver.findElement(By.cssSelector('[data-testing-id="data-id1"]'))!= null){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
}
Java String(s) can't be done in single quotes. This,
By.cssSelector('[data-testing-id="data-id1"]')
Should be (escaping the double quotes),
By.cssSelector("[data-testing-id=\"data-id1\"]")
or with single quotes inside the double quotes, like
By.cssSelector("[data-testing-id='data-id1']")
You should use
"[data-testing-id=\"data-id1\"]"
for(int i = 1; i < 1001; i++)
i++ is another way to add an increment of 1 to i for each loop... And a wee bit easier than typing (i = i + 1)
I am new to Java and not sure if that was worth your time reading, but I think its pretty cool and might make your code prettier...