• 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

containsAll is not working

 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

Could someone please explain me why when I have

and

operation

returns false ??

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post code that compiles and executes and shows the problem?
The posted code does not compile.


Note: Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.
 
Oleksii Movchan
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you post code that compiles and executes and shows the problem?



Sure. This method checks if list (arr) contains all rotations of the string (str). I create additional array StringBuilder[] which contains all possible rotations.

 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and the List<String> does not contain any of the elements of a StringBuilder[] nor of the List made from it.
 
Campbell Ritchie
Marshal
Posts: 79475
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have all sorts of confusing features in that method. Calling a List arr is confusing, for a start.
Don't use String#equals(""); use String#isEmpty(). That is probably incorrect, that your List doesn't contain any empty Strings. The null check shou‍ld precede that line, otherwise there is a risk of a null pointer exception.
You seem to be doing something strange with those StringBuilders; are you trying to create Strings of differing lengths?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The posted code does not compile so it can be executed for testing.  There needs to be a class defined and a main method.
Also the args for the method need to be given values to call the method.
 
Oleksii Movchan
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not posting all the code from the beginning.
Campbell Ritchie, I need StringBuilder[] to create an array of all rotations of a word. If we have "1234" String, its rotations will be "1234","4123","3412","2341".
I decided to use StringBuilder[] instead of String[] because I need to concatenate string a few times.


So, how would you compare StringBuilder[] and List<Strings>? Or there is no additional function for this kind of comparing?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you miss this?


Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.



Try this:

 
Oleksii Movchan
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you miss this?

Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.



Yes, thanks. So actually there is no shorter equivalent of this code?
 
author & internet detective
Posts: 41919
910
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Movchan wrote: So actually there is no shorter equivalent of this code?



If you use Java 8 streams, there is:
 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Movchan wrote:






that code will never compile.  List is an interface  you must declare  a class that implements  an interface to use for your list of strings such as ArrayList  or Vector or any other class that implements the list interface.  basic  arrays  do not implement  the list interface so cannot be declared like that. I would do  List<String> string = new ArrayList<String>();  and then call  the string. add("string"); method to add strings to it.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic