Java for loop - code efficiency - java

I have an array of 10,000 elements and i want to loop through and find a particular number say '6573'.
Example
for(int i=0;i<=user.length;i++)
{
if(user[i]=="6573")
System.out.println("Found it!!!");
}
Could you please suggest a way to improve the performance of the code.
Thanks

Use .equals instead of == to compare strings
Break when a match is found
he end condition must be i < user.length to prevent ArrayIndexOutOfBoundsException:
--
for(int i = 0; i < user.length; i++) {
if("6573".equals(user[i])) {
System.out.println("Found it!!!");
break;
}
}
Note that I inverted the .equals() call to prevent NullPointerException if the array contains null values.

If you need to do it once then that's about it. If you try to find several users in that list then you could use a set for O(1) search:
Set<String> set = new HashSet<>(Arrays.asList(user));
if(set.contains("6573"))
System.out.println("Found it!!!");
And it may actually make sense to store your users directly in that set in the first place instead of using an array.

if the array elements is in order(sorted) then u can use the binary search .. it will increase the performance of your program.

Sort the Array using sort() (efficiency O(n Log n) and use binary search (O(log n) ) .. I think this will be more efficient than your current efficency i.e, O(n).. Just giving details of #abhi's answer...

try like this
int index = -1;
boolean found = false;
for(int i = 0; i < array.length; i++)
{
if(array[i].equalsIgnoreCase(userInput))
{
index = i;
found = true;
break;
}
}

you could use multi-threading and make for example 2 pointers, one travel from the start of the array and one form the end till the middle of the array.
It will use more processing but less time.

Related

Can`t compare ArrayList values in Java

Im doing a Java course and in one exercise I have to create three ArrayLists, ask the user to fill the first two with Integers, and then compare both ArrayLists.
The values that donĀ“t repeat are added to the third ArrayList. I already declared the ArrayLists, used Scanner to allow the user to fill the ArrayLists, and that part works.
The problem comes when I try to compare both ArrayLists. I get all sort of alerts in this line ("the if statement is redundant", "Integer values compared using == or !=","Flip operands of the binary operator", "Invert if").
I suspect that what I wrote after the if statement is not very clean, and that I could get some comments about that (Im not an expert in Java), but I do not understand the alerts that the IDE displays. The code compiles and runs just fine until it hits the nested loops. Please help! Thanks.
//Checking for values that dont repeat
for(int i=0;i<listVector1.size();i++){
for(int j=0;j<listVector2.size();i++){
if(listVector1.get(i)==listVector2.get(j)){//Im getting an alert here
repeats=true; //this boolean was previously declared
} else {
repeats=false;
}
if(repeats==false){
int newValue=listVector1.get(i);
listVector3.add(newValue);
}
}
}
First of all, you have a mistake in the second for loop. I expect you want increment j.
Second is comparing you must explicit cast your values from the array or use function equals.
Third your if statement must be out of your second loop. Because I expect you want to add number in third array only one time as it you find.
for(int i = 0; i < listVector1.size(); i++) {
for(int j = 0; j < listVector2.size(); j++) {
if (listVector1.get(i).equals(listVector2.get(j))) {
repeats = true;
break;
} else {
repeats = false;
}
}
if(!repeats){
int newValue=listVector1.get(i);
listVector3.add(newValue);
}
}
This is the real problem here.
Integer values compared using == or !=
The == operator compares the two object's reference. But what you actually want to do is compare the values stored in the reference.
So, you need to use the equals operator.
Or you could explicitly cast one of the values to int and use == on the values like
if(listVector1.get(i) == ((int)listVector2.get(j))){
repeats=true;
} else {
repeats=false;
}
For more reading, you'd google difference between == and equals operator.

How do I access and manipulate individual values in a String Array when using a while and for loop

Very new to Java: Trying to learn it.
I created an Array and would like to access individual components of the array.
The first issue I am having is how to I print the array as a batch or the whole array as indicated below? For example: on the last value MyValue4 I added a line break so that when the values are printed, the output will look like this: There has to be a better way to do this?
MyValue1
MyValue2
MyValue3
MyValue4
MyValue1
MyValue2
MyValue3
MyValue4
The next thing I need to do is, manipulate or replace a value with something else, example: MyValue with MyValx, when the repeat variable is at a certain number or value.
So when the repeat variable reaches 3 change my value to something else and then change back when it reaches 6.
I am familiar with the Replace method, I am just not sure how to put this all together.
I am having trouble with changing just parts of the array with the while and for loop in the mix.
My Code:
public static String[] MyArray() {
String MyValues[] = { "MyValue1", "MyValue2", "MyValue3", "MyValue4\n" };
return MyValues;
}
public static void main(String[] args) {
int repeat = 0;
while (repeat < 7) {
for (String lines : MyArray()) {
System.out.println(lines);
}
repeat = repeat + 1;
if (repeat == 7) {
break;
}
}
}
Maybe to use for cycle to be shorter:
for (int i = 0; i < 7; i++) {
for (String lines : MyArray()) {
// Changes depended by values.
if (i > 3) {
lines = MyValx;
}
System.out.println(lines); // to have `\n` effect
}
System.out.println();
}
And BTW variables will start in lower case and not end withenter (\n). So use:
String myValues[] = {"MyValue1", "MyValue2", "MyValue3", "MyValue4"};
instead of:
String MyValues[] = { "MyValue1", "MyValue2", "MyValue3", "MyValue4\n" };
and add System.out.println(); after eache inside cycle instead of this:
MyValues[n] = "value";
where n is the position in the array.
You may consider using System.out.println() without any argument for printing an empty line instead of inserting new-line characters in your data.
You already know the for-each loop, but consider a count-controlled loop, such as
for (int i = 0; i < lines.length; i++) {
...
}
There you can use i for accessing your array as well as for deciding for further actions.
Replacing array items based on a number in a string might be a bit trickier. A regular expression will definitely do the job, if you are familiar with that. If not, I can recommend learning this, because it will sure be useful in future situations.
A simpler approach might be using
int a = Integer.parseInt("123"); // returns 123 as integer
but that only works on strings, which contain pure numbers (positive and negative). It won't work with abc123. This will throw an exception.
These are some ideas, you might try out and experiment with. Also use the documentation excessively. ;-)

Get similar part of string array items

I have an array of Strings:
qTrees[0] = "023012311312201123123130110332";
qTrees[1] = "023012311130023103123130110332";
qTrees[2] = "023013200020123103123130110333";
qTrees[3] = "023013200202301123123130110333";
Using this cycle I'm trying to retrieve similar part from them:
String similarPart = "";
for (int i = 0; i < qTrees[0].length(); i++){
if (qTrees[0].charAt(i) == qTrees[1].charAt(i) &&
qTrees[1].charAt(i) == qTrees[2].charAt(i) &&
qTrees[2].charAt(i) == qTrees[3].charAt(i) ){
similarPart += qTrees[0].charAt(i);
} else {
break;
}
}
But this is wrong. As you see it will return only "02301", but the deeper similarity is possible.
Please suggest me a better way to do it. Thanks.
You need to better define what you are trying to achieve. Do you want to:
find the longest common starting sequence between any two entries in the array;
find the longest common starting sequence across all of the entries in the array;
find the longest common sequence (i.e. same characters in same position) between any two entries;
find the longest common sequence across all entries in the array.
All of these will give slightly different approaches, but it will all boil down to correctly using break and continue in your loops.
Remove the else part in your code. Then it will check until the end of the string.
The code :
for (int i = 0; i < qTrees[0].length(); i++){
if (qTrees[0].charAt(i) == qTrees[1].charAt(i) &&
qTrees[1].charAt(i) == qTrees[2].charAt(i) &&
qTrees[2].charAt(i) == qTrees[3].charAt(i) ){
similarPart += qTrees[0].charAt(i);
}
}

startsWith(String) method and arrays

I have to take a string and convert the string to piglatin. There are three rules to piglatin, one of them being:
if the english word starts with a vowel return the english word + "yay" for the piglatin version.
So i tried doing this honestly expecting to get an error because the startsWith() method takes a string for parameters and not an array.
public String pigLatinize(String p){
if(pigLatRules(p) == 0){
return p + "yay";
}
}
public int pigLatRules(String r){
String vowel[] = {"a","e","i","o","u","A","E","I","O","U"};
if(r.startsWith(vowel)){
return 0;
}
}
but if i can't use an array i'd have to do something like this
if(r.startsWith("a")||r.startsWith("A")....);
return 0;
and test for every single vowel not including y which would take up a very large amount of space, and just personally I would think it would look rather messy.
As i write this i'm thinking of somehow testing it through iteration.
String vowel[] = new String[10];
for(i = 0; i<vowel[]; i++){
if(r.startsWith(vowel[i]){
return 0;
}
I don't know if that attempt at iteration even makes sense though.
Your code:
String vowel[] = new String[10];
for(i = 0; i<vowel[]; i++){
if(r.startsWith(vowel[i]){
return 0;
}
}
Is actually really close to a solution that should work (assuming you actually put some values in the array).
What values do you need to put in it, well as you mentioned you can populate the array with all the possible values for vowels. Those of course being
String[] vowel={"a","A","e","E","i","I","o","O","u","U"};
now you have this you would want to loop (as you worked out) over the array and do your check:
public int pigLatRules(String r){
final String[] vowels={"a","A","e","E","i","I","o","O","u","U"};
for(int i = 0; i< vowels.length; i++){
if(r.startsWith(vowels[i])){
return 0;
}
}
return 1;
}
There are some improvements you can make to this though. Some are best practice some are just choice, some are performance.
As for a best practice, You are currently returning an int from this function. You would be best to change the result of this function to be a boolean value (I recommend looking them up if you have not encountered them).
As for a choice you say you do not like having to have an array with the upercase and lowercase vowels in. Well here is a little bit of information. Strings have lots of methods on them http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html one of them is toLowerCase() which as you can guess lowercases a whole string. if you do this to the work you pass in to your function, you cut the amount of checks you need to do in half.
There is lots more you cam get into but this is just a little bit.
Put all those characters in a HashSet and then just perform a lookup to see if the character is valid or not and return 0 accordingly.
Please go through some example on HashSet insert/lookup. It should be straightforward.
Hope this helps.
Put all the vowels in a string, grab the first char in the word you are testing and just see if your char is in the string of all vowels.

How to remove strings of certain lengths

So I have this array, and I want to delete strings that are 2 or 4 characters in length (strings that contain 2 or 4 characters). I am doing this method, and it doesn't work, even though logically, it SHOULD work.
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add("This");
list.add("is");
list.add("a");
list.add("test");
for (int i=0; i<list.size(); i++)
{
if(list.get(i).length()==2 || list.get(i).length()==4)
{
list.remove(i);
}
}
}
I'd like to stick to this method of doing it. Can you please give me some suggestions as to how to correct this code?
The output of this code when I run it is:
[is, a]
Even though I want the output to be
[a]
because "is" is 2 characters long.
The list is changing. Iterate from last element to first or use iterator.
PeterPeiGuo is right - you are removing elements which is shifting your index.
This is a prime candidate for an iterator.
Iterator<String> it = list.iterator();
while(it.hasNext()) {
String val = it.next();
if(val.length() == 4 || val.length() == 2) {
it.remove();
}
}
Another option for it:
When you remove one, decrease your index by 1.
By the way, it works, but is not a good coding style.
for (int i=0; i<list.size(); i++)
{
if(list.get(i).length()==2 || list.get(i).length()==4)
{
list.remove(i);
i--;
}
}
Deleting things from the list changes the indexes of the remaining things in the list.
When your code runs, in the first iteration, i is 0 and it deletes the "this" entry at 0.
On the second iteration i is 1 and thus it doesn't check the value at 0, which is now "is" because the "this" was removed.
As PeterPeiGui says in his answer, you can work around it in this particular case just by going backward, but traversing a collection and mutating it simultaneously always has a risk of introducing plenty of confusion.

Categories