Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Arraylist problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The compareTriplets function will compare between array list of a and b. Easch array list has 3 elements.  
if a>b then score for a is increased by 1, else if a==b do nothing if a<b then score for b is increased by 1
Here, I want to return an array list of compareTriplets function which will have 2 integer values of a and b. Then use this array list in the main function.

I'm not able to return an  ArrayList from the compareTriplets function. can you please give me a hint on that.

Thanks.


 
Bartender
Posts: 1249
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kabi Rabbi wrote:I'm not able to return an  ArrayList from the compareTriplets function.

  • Look at the method compareTriplets carefully, you are returning an array of int instead of ArrayList.

  • Was that the only error? I don't think so since
  • You haven't imported List, ArrayList, BufferedReader etc.
  • You can't compare two Lists elements directly using List variable like in the compareTriplets method
  • you have to use get(int index) method to access an element at the specified position in List.
  • You misplaced the code tags, your code should be between the code tags. How to use code tags
  • Example:Since you are new I'm putting code tags around your code.
  • Code indentation makes It easier to understand It's flow so always format code properly.


  •  
    Ganesh Patekar
    Bartender
    Posts: 1249
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's always better to use braces though The if-then-else Statement has only one statement.
    Instead of use
     
    kabi Rabbi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much for your explanation and help.  How can I return two values as an arraylist where I have counter1 and counter2 values?
    I want to store it in arraylist and want to return that arraylist from Comparetriplets method.





     
    Marshal
    Posts: 74059
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would suggest you are better off creating an IntegerTriplet class or similar, with comparison methods. Make that method return a List<Integer>; it is probably a good idea to make that List immutable or read‑only.
     
    Ganesh Patekar
    Bartender
    Posts: 1249
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
  • Local variables can't have same name. In method compareTriplets you have counter1, counter2 named variables of type int as well as List<Integer> which gives compile time error.
  • Always post complete compiler error message ( In text only, no images Or videos ) if code doesn't compile. If runs successfully and not expected output then post the current output as well as the output you are expecting.
  • You don't need to put hard coded index value to access element of List<Integer> a and List<Integer> b like you did in your code.
  • You have for loop ranging index values of j from 0-2 so use j variable as an Index like
  • You can use method of Arrays class i.e. Arrays.asList() to return an array as List in method compareTriplets.
  • You don't need to create another two variables of List<Integer> type as in your case you tried to create List<Integer> counter1 and counter2.
  •  
    Campbell Ritchie
    Marshal
    Posts: 74059
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know it is a bit late, but the old Sun Style Guide suggest you avoid constructs like if (b) return x; else return y; It recommends you use the ?: operator instead. Note that the else keyword is redundant in that sort of construct.
    The ?: operator can be nested inside itself, but you need to be very careful to indent the code and maintain legibility.
     
    kabi Rabbi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you so much Ganesh and Campbell.
     
    kabi Rabbi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Now the error is run time error. Exception in thread "main" java.lang.NullPointerException at Solution.main(Solution.java:82).
    And I'm not sure about the return of arraylist. Is that correct? Can you please help me regarding this?

    Thanks






     
    Campbell Ritchie
    Marshal
    Posts: 74059
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your main method is much much too long. Don't put all the logic in it; the ideal length of a main method is one statement.
    Don't use multiple write instructions; that will risk slow execution. Use a StringBuilder or a StringJoiner to collect the text and write it all at once. A StringJoiner will obviate the need to test whether you have reached the end of the series, and you can simply truncate a StringBuilder by 1 character.
    Don't close your reader and writer explicitly; use try with resources instead.
    I also think you need separate reading and writing methods.
    I wouldn't use a reader and Integer#parseInt; I would use a Scanner.
    There is something wrong about your using a regular expression to trim the String when you could use String#trim instead. In fact if you split on multiple whitespace ("\\s+"), you may not need to trim the String at all.
    There seems to be a serious logic error in your method creating the List. Apart from the fact that the method doesn't look at all object‑oriented, you have a situation whereby you can return null; I shall leave you to work out which circumstances will create a null there. Line 44.
     
    Sheriff
    Posts: 8064
    569
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Lines 20 - 29

    Why do you have 3 checks of the same kind and same body?

    Lines 30 - 42

    Why do you have 3 checks of the same kind and same body?
     
    Ganesh Patekar
    Bartender
    Posts: 1249
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why are you returning null when elements of a and b are same, here on line 43-45

    Suppose you have elements of a and b as follow
    Indexabcounter1counter2
    ---00
    01100
    11010
    25220

    in above scenario your counter1 should be 2 and counter2 should be 0 but in your code when elements of a and b are same instead of doing nothing you are returning null value to the place where this compareTriplets method was invoked from i.e on line 80
    which assigns null value to result variable.
     
    kabi Rabbi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much. I was able to fix the issue.
     
    Ganesh Patekar
    Bartender
    Posts: 1249
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    kabi Rabbi wrote:Thank you very much. I was able to fix the issue.

    Will be helpful to others who are following this thread If you post the updated code.  
     
    Arthur, where are your pants? Check under this tiny ad.
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic