I am using an if statement as shown below,
if(sign.size()==0)
Here sign is of the type ArrayList<Character>
I am trying to add a char to the ArrayList
But its not working. Is there anything wrong with my if statement?
I also tried the same with an ArrayList<doubler>, this time I could get into the if statement.
Is there anything wrong with the if statement?
I am having very difficult time understanding what you are saying, but it sounds like you are trying to figure out how to increase the list capacity. You do not need to manually do this. Simply add items using add() method and the list will re-size itself as appropriate.
Try if(sign.isEmpty()) instead. Also make sure you you're using good code style with {'s where needed. ArrayLists will adjust their own size to accommodate what you put in, so I'm not sure you even need this check.
You commented on an answer and said,
i want to get in the if only if i have 1 char in the array... but i does not get in to the if.... way if i have only one index
If you want to enter the IF only when there is one char, then you'd want
IF(sign.size() == 1){
...some code
}
Otherwise, if you want to enter the IF when size is 0, use isEmpty.
As you said
I want to get in the if only if I have
1 char in the array... but it does not
get in to the if.... what if i have
only one index?
If you are trying to enter the if statement only when there is only one char in the ArrayList then you can use
if(sign.size() == 1){
//your code
}
The condition will only be true when the size of the ArrayList is equal to 1.
Related
I have two dimensional arrayList, and I want to replace the that is set in the program. The problem is, if i replace the value of arrayList by "ONE", it will work, if I replace it by another value bigger than one, the program enters into a loop. I am using the following syntax:
arrayList.get(index).set(index2,VALUE)
Basically this shouldn't be happening, cause I am not changing anything within the loops, or?
My Questions is WHY? and How to fix it?
the code that is (i assume) producing the problems is:
if(mark.get(index1).get(index2) == 1 && mark.get(i-1).get(j) != 1){
// Replace the value
mark.get(i-1).set(j,1);
flag = true;
}
EDIT: I removed my code, cause it gave the impression I wanted help with the code, i had posted it so that you know what I was talking about. thanks
Got what the problem was! All the initials indexes had a value of ZERO, and I had a condition that was checking if it was not ONE(assuming it will be zero anyway). if i changed it to any other value than ONE then in the next repetitions the condition would have been voided.
This question already has answers here:
for loop VS while loop in programming languages, c++/java?
(9 answers)
Closed 9 years ago.
Some iteration code is written like this
while(true) for(int i = 0 ; i<=4;++i)
{
{
System.out.println(i); System.out.println(i);
if(i==4)
break;
}
}
both have the same logic inside it take it as assumption. So which one will be faster, better to use in code and what are the implication of using any one of it??
The first one is an endless loop :) (depending on the i before the loop) The second one does (almost) what you expect. Go grab a good manual about your programming language, it will explain the difference.
On another level: Loop optimizations are much better suited to for loops (AFAIK) so a "clever" compiler might generate better code for the "for" loop. http://en.wikipedia.org/wiki/Loop_optimization
They are totally different, but if their behavior was the same, never use break; in such situations.
The second bucle is more clear, when you have to take into consideration performance, you don't have to do it in retreat of clean code.
The second bucle is self-documented while the first you need to add a reason to add the break in order to be understandable.
to end the while loop on the left you have to incriment i
EDIT : Assuming i is less that 4 when the loop starts
For that purpose, use a for one, since you need the value of i. It is more understanding for maintenance. Anyway, the compiler will optimize both codes and runtime may be the same.
Considering your while is not an infinite loop there is no difference as far as performance is concerned. However, I personally prefer for loop as it is cleaner. Consider this:
int i = 0;
while (true) {
doSomething(i++);
if (i == 4)
break;
}
which looks cleaner with
for (int i = 0; i <= 4; i++)
doSomething(i);
Im using for whenever I know the number of itterations in advance.
If you know how many time you want to do some operation you have to use for loop otherwise use while loop.
For example:
If you have some array or arraylist then suppose you want to add all
elemnts of it then you should use for loop because you know size of
arraylist by size() function and know the size of array by length
attribute.
If you have resultset and you want to retrieve all rows from it then
you should use while loop because you don't know how many element in
resultset object.
Neglecting the typo of your question,Assuming that you want to know which one i choose when Both loops are essentially doing the same thing.
The answer is you can go for any one.
But it's better to use for loop if you know how many times you want to run a loop as you can set the condition during initialization. And in future also it will be easy to change it if you want to extend the iteration.
You can prefer while if you are not sure for how much iteration you want. In your case i will go with for loop
Note: the answer is opinion based.
i have 3 textboxes. One textbox contains the some integer, eg:2 What i want to do is i want to iterate the loop 2 times.. in every iteration i want to print the value in textboxes. I am clearing the textbox before going to the next iteration. After 2 iterations i want to make the textboxes editable false. But the problem is the loop iterates 2 times but the value in textbox is printed only for the 1st iteration. I wrote the following code
int i=1;
do
{
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
i++;
if(i>nop1)
{
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
break;
}
}while(i<=nop1);
The output i get right now is
neha
8798
1
2
Can anyone tell any alternative to this or please help
I am clearing the textbox before going to the next iteration
There, you solved your own problem!
Its better to use for loop instead of do/while for your code. Refactor your code like this:
for (int i=1; i<=nop1; i++) {
System.out.println(ext_people_name.getText());
System.out.println(ext_contact_num.getText());
System.out.println(i);
ext_people_name.setText("");
ext_contact_num.setText("");
};
ext_people_name.setEditable(false);
ext_contact_num.setEditable(false);
You gave the answer yourself - you CLEAR the textbox before the next iteration. So, it does not contain a value, so it won't print a value.
What you need to do, is to redesign what you are doing and break it into smaller steps. Instead of trying to use one loop to do everything, break it down into smaller loops.
I also do not quite understand what you want to do precisely. Why is the textboxes cleared before the next iteration?
If you only want to display the textbox values twice, and then clear them and make them uneditable, do that in separate steps.
Alternatively, edit your original question and add some more context that we can understand it better.
You are clearing the value in the textbox after each iteration, this make the value empty and prints nothing
System.out.println("some "+ext_people_name.getText());
System.out.println("some "+ext_contact_num.getText());
use above code that, you can come across that you are overwirting the result after iteration
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android (method for checking an arrays for repeated numbers?)
I've just asked a question and got a few answers and i was very happy to, but there were very complicated answers, I'm quite new to android so can some one maybe give me some example code or some think explained not the complicated. I've tried there code and tried to make sense of it but i cant.
here is the question....
could any one help me. i am making an app, and in the java, numbers are send to a int array and i need to check if any of the numbers in the array repeated and if there are to call a method or something like that. Is there a method to check this or something similar? or would i have to do it using loops and if statements, which i have tried but is getting a bit long and confusing. Any advice would be great, thanks.
int test[] = {0,0,0,0,0,0,0}; (The Array)
(A method to check if any of the arrays numbers are repeated)
First don't make double topic.
Second you are searching for a Java answer not related to Android.
I think that maybe it's better if you first learn java (or other language like).
I would store the items in a Set if you do not want them to repeat. If add returns false then you have a repeating number
Set uniqueItems = new HashSet();
for(int i=0;i<test.length;i++)
if(!uniqueItems.add(test[a]))
System.out.println("The item is already in the set");
First, sort the array. Then search through the array comparing each node to the node on either of it's sides. Or you could store the data in a Set which cannot have duplicates.
Arrays.asList(test).contains(valueYouWantCheck).
If you want to find out for each and every value in test array, Yes I think you need to loop the array.
Hi
I have a question that in the bellow code do we need to write n--; ?
int n = pointList.size();
for(int i=pointList.size()-1;i>=0;i--){
for(int j=0;j<list.size();j++){
if(pointList.get(i).equals(list.get(j))){
pointList.remove(i);
n--;
}
}
}
Also list is an arrayList.
thanks.
ArrayList is backed by an array that starts at a fixed size and is only resized if it needs a larger size. As far as I'm aware, you can only shrink the size of ArrayList's backing store using trimToSize()
If the size of the store is important, consider using a LinkedList instead.
Note:
If I'm reading your code correctly, you can replace all the code you posted with just this instead:
pointList.removeAll(list);
No, you don't, since n is never read anywhere.
I believe, this is a relict from earlier, where the developer added this either to use n in a debug.print statement, or in an assertion.
Since there isn't really a question here I will take a guess. My guess is that the declaration of int n = pointList.size(); is redundant. Since you will always have pointList it will know its size. Also I am willing to bet that is going to throw a concurrent modification exception.
Because the ArrayList will decrease it's size each time you remove an item you don't have to use n at all.
if you do the following after each iteration, it will achieve the same result:
pointList.remove(pointList.size() - 1);
The -1 is there because obviously arrays start with 0.
Ok, I guess that you want to keep the size of the pointList stored in n up to date. In this case you have to decrease n. But you can simply assign n after the double for loop.