• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simplifying this code  RSS feed

 
Wally Leb
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I transferred from a school where I took intro to C++ for 16 weeks to a new school that does Java instead. I got thrown into Java II, and I'm just trying to get up to speed to catch up with the rest of class by this weekend. I still have a couple more chapters to go, but I think I would be able to understand some things about Java much better with a little help for the following code:



This program allows a user to enter the dimensions of a 2D matrix, enter the values to fill up the matrix, and then return the array location of the largest value. This program works, but it returns the location as doubles instead of ints; I tried multiple things, but I couldn't get it to change. Also, I think I made it more complicated by not calling on the right (libraries? I'm not sure what they're called).

If somebody could show me a more efficient way to code this in Java, I would be extremely grateful; it would allow me to deconstruct it and get a better idea of what other programs could/should look like.

Also, where can I find a list of the java.util.blahs and how to use them? What are they called? Libraries?

Any help I get will be greatly appreciated.


Thanks,
Wally
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wally,
Welcome to CodeRanch! The libraries/APIs are in the JavaDoc.

I copied your code into my workspace and did a few refactoring to make it more Java like. I'm listing the changes I made along with what it looks like at the end.

  • In Java, method names begin with lowercase by convention
  • Since the double array isn't modified in main(), it makes more sense to have the method that fills it create it. And return that value to main
  • Passing rows/columns to locateLargest() is redundant. You ahve this information already in the array. (Presuming the array isn't empty, but then you have larger problems
  • Removed parens around smallArray




  • Also, suggested improvements that have nothing to do with Java:
  • Consider adding validation - the program currently accepts -1 as a size and then blows up
  • If you've learned about objects, consider making an instance variable for myArray. You'd need to instantiate the class rather than have everything be static for this
  •  
    Carey Brown
    Saloon Keeper
    Posts: 3323
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You declared smallArray as a double[], replace it with int[], and change the method return type accordingly.
     
    Wally Leb
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanna and Carey,

    Thanks for your input. I guess the code isn't as jacked up as I thought, which is kind of a relief.

    Jeanne, in the following code that you amended, myArray.length = rows and myArray[0].length = columns?

     
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 37496
    545
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wally Leb wrote:Jeanne, in the following code that you amended, myArray.length = rows and myArray[0].length = columns?

    Correct. A 2D array is an array of rows. Since you created the array, you know each ros is the same # of columns.
     
    Wally Leb
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Once again, thanks very much!

    -Wally
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:
    Wally Leb wrote:Jeanne, in the following code that you amended, myArray.length = rows and myArray[0].length = columns?

    Correct. A 2D array is an array of rows. Since you created the array, you know each ros is the same # of columns.

    @Wally: But it doesn't have to; and that's why it's so important to use the length attribute.

    A Java array only has one dimension, but Java arrays are also fully-fledged objects; so each "element" in an array can itself be an array. Indeed, in a
      int[][]
    each "element" (or "row") is actually an int[], and there's nothing to stop you making them different lengths if you want.

    Therefore, the correct way to process a "2D" array is:However, it may also be worth mentioning that there is another form of for loop called the "for-each" loop, which can be very useful with arrays - especially "multi-D" ones, viz:Much simpler, eh? And very possibly faster too. The only things to note are that:
    1. You can't use that form for changing elements; only for reading them.
    2. You have to keep track of indexes separately.

    But you could certainly use it for your locateLargest() method, viz:You may notice that I set 'smallArray' to null initially. This is simply to guard against the fact that 'myArray' doesn't contain ANY rows - it's perfectly possible to create a 0-length array in Java - in which case, there can't be a "max" element, can there?

    Now in your case, that may never happen, but it's always worth thinking about these things when you write (or test) a program.

    HIH

    Winston

    PS: I broke up line 80 in your OP because it was too long. Please read the link before you post more code.
    Thanks.
     
    Wally Leb
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston,

    Sorry about the long length; I'll be sure to watch that next time! Also, thanks for the tip about 80 characters; I'll have to work on that, lol.
    I really appreciate the input, and now I understand Java arrays much better.

    I do have a question about the for-each loop, though. Could you write me a statement in English for what this is actually saying?



    I really appreciate all the help from the community!


    Thanks,
    Wally
     
    Rico Felix
    Ranch Hand
    Posts: 411
    5
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wally Leb wrote:I do have a question about the for-each loop, though. Could you write me a statement in English for what this is actually saying?

    Since you are dealing with a 2D array, it simply mean for every element within the set of sets, perform a specified operation...
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wally Leb wrote:Could you write me a statement in English for what this is actually saying?

    Well, the best I can come up with is:

    "Process each element in my 2-D array, in order from 'top-left' ([0][0]) to 'bottom-right'; even if the rows have different lengths."

    And inside the inner loop (at least the one I wrote above), you simply use 'element' instead of array[i][j].

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why do you have lots of Scanner objects? You only need one Scanner object. Put it into a utility class and give it static methods to read an int. Just beware of the nextLine method. Look here for a bit more detail.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!