I have 3 arrays.
array1 has a b(suppose)
array2 has a
array 3 has b
how to find in which array ,array1 elements are present?
here all 3 array may or may not be of same length.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have 3 arrays.
array1 has a b(suppose)
array2 has a
array 3 has b
how to find in which array ,array1 elements are present?
here all 3 array may or may not be of same length.
I think you have to code some loops and compare the elements of the arrays.how to find in which array ,array1 elements are present?
What have you tried?l
If you don't understand my answer, don't ignore it, ask a question.
ya i have tried with loops.since arrays may be of different length am getting ArrayIndexOutOfBoundException.So am not able to code.
The code needs to test that the index it is using does not go past the end of the array.
If you don't understand my answer, don't ignore it, ask a question.
Based on post #3 it sounds like you are comparing arrays index for index. Are you looking for exact matches in the same spot or just if the item is in the other array anywhere? If that is the case then it sounds like you are looking for some nested for loops. As long as you put the length of the array as part of the condition for looping you should never get an ArrayIndexOutOfBoundException.
Do you have code you can show or a better description of the problem you are trying to solve?
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
Ya i want to know items in one array, say array1 is present in which array.Not the order.I just want to know which array contains elements of array1.Am sorry am not able to code properly for this.So can't show you my code.It's incorrect.
That's what we're here for. Show us the incorrect code and we will help you work through it. Trust me, we see some god aweful code here and you wont be ridiculed.
Anyways, sounds like you just need some nested for loops. Try listing what needs to happen in basic english. This always helps me notice patterns that can be applied as loops or conditional branches.
Something like: FOR EACH item in this array, look at EACH item in this other array and determine IF they are equal. Hey, look at those key words. Looks like i can apply that directly to my code.
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
here 'array' has some items.i want to check array[i]th item is in int_array or float_array.public class arrays { public static void main(String[] args) { String int_array[]=new String[20];//int_array has a b c elements String float_array[]=new String[20];//float_array has a1 b1 elements String array[]=new String[20];//array has a b b1 elements StringBuffer sb = new StringBuffer(); for(int i=0;i<array.length;i++) { for(int j=0;j<int_array.length;j++) { if(array[i].contains(int_array[j])) { sb.append("%d"); break; } } for(int k=0;k<float_array.length;k++) { if(array[i].contains(float_array[k])) { sb.append("%f"); break; } } } } }
The basic format of the program is what you're looking for. Make sure your if compare is working as you would expect it. I dont think .contains is going to give you your desired result.
PS: My old Comp Sci teacher would crucify you for putting a break in a for loop although i would argue it is a legitimate strategy.
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
ya .contains doesn't work out,instead i used .matches.It's working fine.And i removed break statement.
i just want to know if my approach in writing code was rite or i need some changes in the code i posted.
The approach works. There are other methods for finding elements in arrays, but that's a lot of computer science theory i dont care to get into.
Continuing in a for loop once an element is found is asinine in my opinion. If i'm looking for butter in the fridge and i find it, i dont continue looking just because there's more stuff in the fridge. One way to break out of a for loop without using an explicit "break" is to add to the conditional check at the top. Almost making it a for loop/do while hybrid. See my example.
boolean found = false; for(int i=0 ; i < array.length && found==false ; i++){ if(array[i] == itemToFind){ found=true; } }
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
shenaz (March 29th, 2013)
ya rite.I will include conditional check in for loop.Thanks alot for responding in early
Any other questions? If not please mark thread as Solved.
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
No more questions regarding this.Thank you.And i have already marked it as solved.