[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Extra juicy! | Home]

Been struggling with this Java code for 3 days now. I want to

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 42
Thread images: 2

File: Java2.jpg (53KB, 1024x768px) Image search: [iqdb] [SauceNao] [Google]
Java2.jpg
53KB, 1024x768px
Been struggling with this Java code for 3 days now.

I want to check how many integer elements of two ArrayLists have the same value.

public static int equals(List<Integer>list1, List<Integer>list2){
int correct=0;

for(int i = 0;i<LENGTH;i++){
if(list1.contains(list1.get(i))&&list2.contains(list2.get(i))){
correct++;
}
}
return correct;
}


How would /g/ do this??? It's the first time im this stuck, have to use ArrayLists since the rest of the code uses that
>>
>>51390463
LENGTH isn't declared
>>
>>51390490
It's declared outside of the method
>>
>>51390508
That's not how java works, also for integer#.

Just use list.lenght()
>>
>>51390556
That's not he problem, the problem is in comparing the two lists.
>>
java a shit
>>
for one, how the fuck do we know they're the same size
>>
>>51390593
Why are you so obsessed with their sizes? They are the same size...
>>
>>51390565
Which case he's even dumber.

He will need a double for loop, and will have to check for dups (if needed)

This is just checking is they have matching integers at the same index
>>
>>51390463
add another for loop

public static int equals(List<Integer>list1, List<Integer>list2){
int correct=0;

for(int i = 0;i<LENGTH;i++){
for(int k = 0;l<LENGTH;k++){
if(list1.contains(list1.get(i))&&list2.contains(list2.get(k))){
correct++;
}
}
return correct;
}
}
>>
instance Eq a => Eq [a] where
(x:xs) = (y:ys) = x == y && xs == ys
[] == [] = True
_ == _ = False
>>
>>51390617
Thank you but that still doesn't work.

The lists are of a fixed size e.g. 5 elements each with a random number value between 1 and 100
>>
Check if the lists have the same length. No- return false.
Sort both lists
Compare the values at each index of the lists. If any differ, return false
Return true
>>
>>51390655
post the output
>>
>>51390655
Why are you using arraylists for integers when they're of a fixed size??????
>>
>>51390672
list1 = {1,2,3}
list2 = {17, 34, 39}

The output becomes 9


>>51390682
Because i need the arraylist functionality
>>
OP HERE: I fixed it now,

for(int i = 0;i<LENGTH;i++){
for(int k = 0;k<LENGTH;k++){
if(list1.get(i)==list2.get(k)){
correct++;
}
}
}
System.out.println(correct);
return correct;



Thank you guys!
>>
should work for unique values

    public static int equals(java.util.List<Integer> list1, java.util.List<Integer> list2){
int correct = 0;

java.util.List<Integer> biggerList = list1.size() > list2.size() ? list1 : list2;
java.util.List<Integer> smallerList = list1.size() > list2.size() ? list2 : list1;

for (int i = 0; i < biggerList.size(); i++) {
if (smallerList.contains( biggerList.get(i) )) {
correct++;
}
}

return correct;
}

public static void main(String[] args) {
java.util.List<Integer>list1 = new java.util.ArrayList<>();
java.util.List<Integer>list2 = new java.util.ArrayList<>();

Collections.addAll(list1, 1, 2, 3, 3, 4, 5);
Collections.addAll(list2, 1, 2, 3);

System.out.println(equals(list1, list2));
>>
File: facepalm1.jpg (111KB, 667x858px) Image search: [iqdb] [SauceNao] [Google]
facepalm1.jpg
111KB, 667x858px
Holy shit this thread is full of retards.
>>
>>51390852
Welcome to /g/
>>
>>51390463
I'm a fulltime java dev and your code disgusts me. Did you come from a fucking python/C background or some shit like that?
>>
>>51391270
>Did you come from a fucking python/C background
Yes, what's wrong with the code? The arraylists?
>>
>>51390463
>java
Found the problem
>>
>>51390725
holy shit what a retard
>>
>>51390604
>Why are you so obsessed with their sizes?

You're obviously a new programmer so I'll excuse your faggot ass this time. It's very important to know an array's size you nigger otherwise you'll go out of bounds and crash the program. And that's not even the worst that can happen.
>>
>>51392052
Oh look another faggot who only complains and doesn't offer a better solution
>>
>>51392057
>array
OP is not using an array though
>>
>>51392082
>sort arrays
>solve in linear time
here, did it.
>>
>>51392106
Oh god you are retarded
>>
If this was PHP I'd just use array_intersect, so use whatever Java's version of that is or search for "Java array intersection".
>>
>>51392126
Enjoy your quadratic algorithm java-tard
>>
>>51392106
This implies the array sizes are the same as the possible values of each element
>>
>>51392168
no, it does not.
>>
>>51392082

Make two sets, SetA, SetB
Intersect SetA with SetB: SetA.retainAll(SetB)
Return length of intersection

Not that hard, ain't it?
>>
>>51390463
Not sure if bait, but I'll take it.

You're over thinking it, bad.

int num = 0;
for(loop through list1)
{
if (list2.contains(list1.get(i)))
num++;
}
return num;

Let the arraylist API do the work for you, that's the point of this exercise.
>>
>>51392224
>SetA.retainAll(SetB)
THANK YOU! Seems like trying to reinvent the wheel with for loops is really bad
>>
>>51391323
Arraylists are fine. It's your code style (jesus fuck you have a spacebar you know) and the way you tried to make this work. It's like you didn't think about it at all.

Also
>list2.contains(list2.get(i))
>applause
>>
>>51390593
Check their lengths dummy.
>>
I remember having to do this exercise to IMPLEMENT the retainAll method. But if that's not what you're trying to do, it sounds like that's all you need. Why are you making this hard on yourself?
>>
>>51390619
that isn't even valid haskell you fuck
inters (x:xs) s m 
| x `elem` s = inters xs s $ x : m
| otherwise = inters xs s m
inters [] _ m = reverse m
>>
>>51392256
like 35 posts later
fucking embarrassing
8/10 because newline bracket
>>
>>51390463
just use a HashSet

public static int equals(List<Integer> list1, List<Integer> list2)
{
HashSet<Integer> hash = new HashSet<Integer>();
hash.addAll(list1);
hash.retainAll(list2);
return hash.size();
}
Thread posts: 42
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]
Please support this website by donating Bitcoins to 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
If a post contains copyrighted or illegal content, please click on that post's [Report] button and fill out a post removal request
All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site. This means that 4Archive shows an archive of their content. If you need information for a Poster - contact them.