• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trying to find union/intersection of arrays containing integers

 
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me open with this: i suck at arrays/programming.

here's my assignment:

Read two sets of numbers from the keyboard.
Print the intersection of the two sets, in any order, without duplicates.
Print the union of the two sets, in any order, without duplicates.


okay, great. so i have this:



doesn't work. no idea why. i barely understand how arrays work. we're not allowed to use Java classes that compute union and intersection, if such classes exist. only integers will be used.

i've struggled with this assignment for hours, scouring the internet, but i just keep finding answers i don't understand - things like something like that. i have no idea here to go from here. i keep finding ways to concatenate strings, but there aren't strings, so it yells at me. i tried converting them to strings, but it made a mess.

while we're at it, if anyone knows some good places for Java tutorials written for baby programmers, i'd love to peruse them. i feel like i'm in over my head most of the time.
 
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have difficulty finding the Arrays.inter() method; there isn't one. It is unusual to do the intersection of two arrays, but it is common to do the intersection of two sets.

Are you supposed to do it from scratch, ie write your own method, or are you supposed to find methods already in existence in the API and use them? If the latter, go through the Java™ Tutorials and read about the operations available on sets, and the set implementations.
As for reading from the keyboard, make a rule that you write each set as a single line of any length, separated by whitespace, like this

123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1

. . . and you can use the String#split method to change that to an array. The link tells you which regular expression you use to match whitespace, but when you quote it as a String literal you will need to double the \ character; not "\???" but "\\???". On each element use the appropriate method of the Integer class to turn it into an int before putting it into the array. Note the array of Strings you get will have a particular length,

If you need to do the operations directly on the arrays, that is very awkward. Is this a Java™ module or an algorithms module?
I suggest you start again from scratch. Reading two arrays of Strings from the keyboard is a good start. Work out how to remove duplicates from each array, which is probably easier if you sort the arrays first. Work out how to copy the non-duplicated part of the array into a new array, which will still be sorted, and then work out how to convert that new array into an int array.
Then use those two arrays to work out how to add them and create a union or to identify common members and create an intersection.

Note if card(s1) = i and card(s2) = j, then card(s1 ∪ s2) ≤ i + j and card(s1 ∪ s2) = i + j - card(s1 ∩ s2). That may help you calculate the sizes of the array required.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i...didn't understand most of that.

i've never seen String#split methods, i'm not sure if it's a Java module or an algorithms module (probably Java?). my prof specifically said we won't cover regular expressions in this class, either. i don't really know how to sort/remove duplicates from an array (but i think my book has info on those two things). we're supposed to do it from scratch, i can say that much.

i've looked at the tutorial set before, but i don't understand most of that, either. sorry, thanks for trying to help.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Euridice Nobium wrote:doesn't work. no idea why. i barely understand how arrays work.


What does "doesn't work" mean exactly? Does the code compile and run, or do you get a compiler error? If so, then what exactly is the error? If it runs but does something that you don't expect, then how does what it really does differ exactly from what you had expected?

Can you be more specific about this - what exactly do you not understand about arrays? Arrays are not that complicated - an array is really just a list of elements, that you can address by an index. See this tutorial for detailed information: Language Basics - Arrays.

Look at lines 33 and 35 in your code:

Do you understand what's happening here? You are taking the value at position 0 in both arrays and adding those two values together. It will just print the sum of the first two values you entered for both the sets of numbers. It doesn't do anything with intersections or unions of the two sets of numbers.

Euridice Nobium wrote:i feel like i'm in over my head most of the time.


Try to focus on one thing at a time, take small steps - don't try to learn too much at a time, because that will be overwhelming and discouraging.
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divide and rule. Write a class which takes a String from the keyboard, and splits it into individual tokens with the String#split method, and the regular expression mentioned, which by the way, you will find if you click on the links from String#split. Remember about doubling the \.
Pass it a String like what I showed earlier, divide it into an array, and print out the individual members of that array on different lines. So my String earlier will be displayed as

123
873
8273
12
9438
73
0
843
05
1
93847634
8423
054
67
4
87
26
9
1

Repeat the procedure, but converting each String to an int and adding 1 before printing it, so you see

124
874
8274
13
etc . . .

Now you can create an int[] array from a keyboard input String. And you have an array exactly the right size, with no blanks anywhere.

Make that the first stage of the problem.
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Euridice Nobium wrote: . . . i've never seen String#split . . .

But some of the text was blue and underlined; it is a link, like this. If you click it, you find something useful (or maybe in some cases, something useless ).
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper de Jong - it compiles and runs. what i meant is that it doesn't do what i want it to do. i understand why line 35 doesn't print what i want it to print, but i'm not sure what to do about it; i just put it there in an attempt to work with what i know.

what i don't understand about arrays is how to use them. so i have these "bins" of whatever, numbers, in this case. i can work with strings because everything we've worked with in class has involved words, not numbers. for instance, i had to write this code:



i'm totally clear as to what's happening here. obviously, though, i can't just make counters for this new problem; i don't want to add the numbers together, i just want to find the union and intersection. i get the concept behind what i'm being asked to do, but i have literally zero idea where to go from what i know already.


Campbell Ritchie, i also don't know what tokens are. or the split method. or regular expressions. or doubling the \.

i'm sorry, i'm not trying to get anyone to do this for me.

here's what i know. i have an array of numbers. i can read those numbers in from the user, and i think the code i posted previously contained those scores the way i wanted them to. now, i have to compare the two sets and output two results. the first result will compare the strings and list which numbers appear in both arrays and nothing else. the second result will combine and display the inputted numbers, getting rid of duplicates.

i can wrap my head around the concept in English, but when i try to find methods and such that correspond with what i have to do, i get confused because the sources i find reference a lot of information i don't understand. that's the real problem here, i guess.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Euridice Nobium wrote: . . . i've never seen String#split . . .

But some of the text was blue and underlined; it is a link, like this. If you click it, you find something useful (or maybe in some cases, something useless ).



yeaahhhhhh, i followed the link, but it was basically meaningless because i don't know how that would look in my code. or any other code for that matter. i've never used regular expressions or anything aside from public static void as a main. i think my prof kind of likes to make us drown a little so that we have to expand our base of knowledge, which is cool, if frustrating. that's how i did the other assignments.

the second link, i can relate to that.
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tokens are bits of writing separated by something else, usually by what we call "whitespace". If you go through any of these posts, each individual word, or number, or operator, counts as a token.
If you look at the String#split method, and click on the link there called "regular expression", you find out what the regular expression for whitespace is. But if you copy anything with a \ into a String literal, you can't write "\???". You have to double the \ to make "\\???". That is because \ is an escaped character.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, okay, that helps a lot, actually. still not clear on this regex business. i did notice that Split lists and to be use for union and intersection, specifically, but even if i did know how to use those, i'm not allowed. i have to do it by hand.

what is a String literal?
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The string "boo:and:foo", for example, yields the following results with these expressions:

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

From the String#split link quoted earlier. That can be translated into code like this

java StringSplitDemo boo:and:foo : o gives 3 tokens.
Splitting with :
boo
and
foo
Splitting with o gives 3 tokens.
b

:and:f

Now try with my earlier list of numbers and " " as arguments, but you need quotes around anything with whitespace in. Now you can see how to split a String into its tokens.

By the way: You can create yourself a set of Strings representing numbers, and do the same with those Strings.

It is not a good idea to put everything into the main() method like that, but it is quicker for iterating the args array.
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"This" is a String literal. So is "this".
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay...i feel dumb, but...how do i use your list of numbers and that space as an argument? where does that go?
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please enter your integers, as many as you like, before the enter key:
123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1↩


where "↩" means enter key.
Now input is equal to "123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1"
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I may have misunderstood your last question. Sorry if I have. Go back to the [ur=https://coderanch.com/forums/posts/list/532900#2416616l]StringSplitDemo[/url] thing I showed you earlier. Try the input as a command-line argument.. Try adding the last bit of code I showed you, and see what happens. . . . and you can try it like this:

java StringSplitDemo "123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1" \s \\s " "
Splitting with s gives . . .
Splitting with \s gives . . .
Splitting with gives . . .
Please enter your integers, as many as you like, before the enter key:
123 873 8273 12 9438 73 0 843 05 1 93847634 8423 054 67 4 87 26 9 1
Try it for yourself ;-)

If you want to see what happens, you will have to try it for yourself!
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, i used the code you told me to use, and i got it to run and compile, but that's not what i need to do. i need to take numbers from the user into the array, and i don't know how to adapt this method. after more head-banging, i have this new code:




this code is simpler and probably less efficient, but it makes sense to me. however, it skips the first two numbers of firstSet even though i need it to print all of the numbers entered. additionally, i still need to find a way to print just the numbers that appear in both sets. if the user enters

1 2 3 -1 for firstSet and
2 3 4 -1 for secondSet, output should be


Union: 1 2 3 4
Intersection: 2 3



right now, i don't have intersection at all, and Union would print 3 4 2 3.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
problem solved! i had a few variables in the wrong places, among other issues. thanks for the help!
 
Campbell Ritchie
Marshal
Posts: 79175
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic