Results 1 to 9 of 9
  1. #1
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Looking for a little guidance on ArrayLists

    So I've had some classes in Java and I'm working on some of my own code. While playing around with ArrayLists I can't help feeling like I'm doing something wrong. It seems there should be a way to condense the following code to be more efficient. Initially all of the ArrayLists will be empty, but then my code will pull players from a list in a simple .txt file using a scanner object.

    Java Code:
    ArrayList <String> centers     = new ArrayList <String>();
    ArrayList <String> forwards    = new ArrayList <String>();
    ArrayList <String> goalies     = new ArrayList <String>();
    ArrayList <String> right wings = new ArrayList <String>();
    ArrayList <String> left wings  = new ArrayList <String>();
    ArrayList <String> coaches     = new ArrayList <String>();
    ArrayList <String> emptyset    = new ArrayList <String>();
    Is it possible to declare the ArrayLists in a single statement as can be done with int objects? For example,
    Java Code:
    int x, y, z = 0;
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Looking for a little guidance on ArrayLists

    Quote Originally Posted by Sizzlewump View Post
    Java Code:
    int x, y, z = 0;
    What does that code do?

    Specifically I'm thinking about the variant:

    Java Code:
    int x, y, z = 1;
    as in

    Java Code:
    public class Test {
      int x, y, z = 1;
    
      public static void main(String[] args) {
        Test test = new Test();
        System.out.println("z = " + test.z);
        System.out.println("x = " + test.x);
      }
    }
    You might also want to think about

    Java Code:
    public class Test {
      public static void main(String[] args) {
        int x, y, z = 0;
        System.out.println("z = " + z);
        System.out.println("x = " + x);
      }
    }
    Last edited by pbrockway2; 04-05-2015 at 05:12 AM.

  3. #3
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Looking for a little guidance on ArrayLists

    Sorry for the confusion. Allow me to clarify, but first I'll answer your question.
    Java Code:
    int x, y, z = 1;
    This code creates 3 variables: x, y, z and initializes them to the value 1. Back to my original idea, can we apply the same logic to an ArrayList or is it possible to Enum the lists somehow? I'm not sure what best practices are. The code is functional as is, I'm simply looking for a way to clean it up. Thanks again!
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Looking for a little guidance on ArrayLists

    First, some syntax suggestions. It is best practice to assign list implementations to their interface type. So

    Java Code:
    List<String> foo = new ArrayList<>();
    Also, notice that the List type is inferred so I just need <> in the new declaration (as of Java 1.7). And you can't have spaces in your variable names.

    You could also use Maps and symbolic references (i.e. Strings) to reference ArrayLists. But there is nothing wrong with the straight forward approach that you are doing.

    Regards,
    Jim
    Last edited by jim829; 04-05-2015 at 05:11 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Looking for a little guidance on ArrayLists

    Not quite. x and y are not initialized. z is initialized to 1. If they were instance fields, then x and y would be 0. You want,
    int x = 1, y = 1, z = 1;

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Looking for a little guidance on ArrayLists

    Okay, thanks Jim. I have tried your suggestion and the code works fine when I change it to:
    Java Code:
    List <String> centers     = new ArrayList <String>();
    List <String> forwards    = new ArrayList <String>();
    List <String> goalies     = new ArrayList <String>();
    List <String> rightWings = new ArrayList <String>();
    List <String> leftWings  = new ArrayList <String>();
    List <String> coaches     = new ArrayList <String>();
    List <String> emptyset    = new ArrayList <String>();
    I believe I'm using Java ver 1.6, but good to know about the shortcut. Again thanks for the help. I guess that's as good as it gets. I was hoping to do something like:
    Java Code:
    List <String> centers, forwards, goalies, rightWings, leftWings, coaches, emptyset = new ArrayList <String>();
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  7. #7
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Looking for a little guidance on ArrayLists

    I see, so it would have to be more like:
    Java Code:
    int x, int y, int z = 1
    Correct?
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Looking for a little guidance on ArrayLists

    Quote Originally Posted by Sizzlewump View Post
    I see, so it would have to be more like:
    Java Code:
    int x, int y, int z = 1
    Correct?
    That doesn't compile (I think. The compiler will tell you for sure!)

    As Jim says "int x, y, z=1;" doesn't initialise x and y at all. That was the point of my first Test class. If you compile and run it you will see that x and y retain their default values of zero. You don't see that with what you posted - "int x, y, z = 0;" - because what you were assigning to z was the default value.

    The second example (again the point was to compile it) won't compile at all. Again because the "int x, y, z = whatever;" construction doesn't initialise the first two variables and, in a method, variables must be initialised before they are used. They don't have a default value.

    I declare variables each on their own line (to help both the /** comments */, and my own brain). But I'll take Jim's word for it: #5 is how you do things on one line. Using two lines you could say:

    Java Code:
    void someMethod() {
      int x, y, z;
      x = y = z = 0;
    }
    That would save you one line, while leading you into temptation:

    Java Code:
    import java.util.ArrayList;
    
    public class Test {
    
      public static void main(String[] args) {
        ArrayList<String> foo, bar;
        foo = bar = new ArrayList<String>();
    
        foo.add("Some foo");
        bar.add("Some bar");
    
        System.out.println("First foo is " + foo.get(0));
        System.out.println("First bar is " + bar.get(0));
      }
    }
    The bottom line is that if you want seven lists you have to use the "new" keyword seven times.
    Last edited by pbrockway2; 04-05-2015 at 07:44 AM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Looking for a little guidance on ArrayLists

    It does look like a better design would be a Team class with fields for the appropriate number of centers, forwards, goalies, right wings, left wings and coaches, and a single ArrayList<Team>.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Hi There! / Need some guidance.
    By NoMan in forum New To Java
    Replies: 8
    Last Post: 04-06-2015, 03:15 PM
  2. Looking for guidance
    By Syphax in forum New To Java
    Replies: 3
    Last Post: 05-30-2012, 08:09 AM
  3. Need some Guidance with SOA
    By Arukas in forum New To Java
    Replies: 1
    Last Post: 02-22-2012, 09:45 AM
  4. Some Guidance
    By bengregg in forum Java Applets
    Replies: 15
    Last Post: 01-11-2011, 10:04 AM
  5. Guidance
    By gizdev in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •