Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Parallel Array Lists?

    I want to have a list with hosts and their corresponding ports, is there a way I could do this besides using a HashMap? The reason I say this is because what if I wanted to add a 3rd object?

    Thanks,
    ~Solarsonic

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    How do you want to access the data?
    With a Map, the key could be the host, and the value could be a list of ports.
    if you want the value to be heterogeneous (ie different types) make your own class to hold the variable bits and pieces and lists that you want to save for each host.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    How do you want to access the data?
    With a Map, the key could be the host, and the value could be a list of ports.
    if you want the value to be heterogeneous (ie different types) make your own class to hold the variable bits and pieces and lists that you want to save for each host.
    I want to be able to do something like this (theoretically):

    Java Code:
    ArrayList<String, Integer, Double, Character> list = new ArrayList<String, Integer, Double, Character>();

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    How would you describe the "theoretical" list you want to create?
    What would an element in the list look like?
    What would be returned if you did a get(ix)?

    As I said before: make your own class to hold the variable bits and pieces and lists that you want to put in container.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    I have to echo Norm, really. Making your own class would probably solve all your problems, and it would make it much easier to add additional data or change the structure at a later date (you wouldn't have to rewrite all the arrays, lists or maps, for instance).

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    My 0.02

    Beware, it you are going to make a class simply to hold a bunch of disparate data then I smell a design flaw.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by Junky View Post
    Beware, it you are going to make a class simply to hold a bunch of disparate data then I smell a design flaw.
    I disagree with that statement pretty strongly. Associating disparate data is pretty much the whole point of OOP.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The whole point of OO design is to hold related data together. For example a Person object holds a name, address, DOB etc. All the information about a person.

    You do not create a class to hold the atomic weight of an atom, the name of your first pet, the interest rate of your home loan and the clock speed of your computer. This is what I mean by disparate data.

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    In this case though, the data is related. It'd be like holding the atom number, the number of protons, the mole-weight and such. I definitely agree with you on not bunching disparate data together, but as far as I understood it, that is not the case here.

  10. #10
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Ok well what I'm using the list for is this:

    I have a Main class and a Proxy (object) class. Every proxy has a host and a port right?

    So in my Main class I'm reading a text file for hosts and ports of proxies, and I want to have a list of hosts and their respective ports. Then, I would create new Proxy objects with the hosts and ports in my list.

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    So, to echo what Toll said earlier, you probably want to use an ArrayList<Proxy>.

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by kjkrum View Post
    So, to echo what Toll said earlier, you probably want to use an ArrayList<Proxy>.
    Well I guess I could create a Proxy object for every line I read, but is there a way I could prepare the whole list before I start creating Proxy objects?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    a way I could prepare the whole list before I start creating Proxy objects
    Can you explain?
    If you don't have any objects yet, how can you prepare the whole list?
    You'd add the objects to the list one by one as you create them.

  14. #14
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Can you explain?
    If you don't have any objects yet, how can you prepare the whole list?
    You'd add the objects to the list one by one as you create them.
    The list isn't a list of objects here. I'm trying to prepare a list in order to create Proxy objects. Just a list of a string and an int, host and port.

    In fact, forget the Proxy objects. I'm trying to get a list of a string and an int.

    Instead of just using two lists - one for the hosts and one for the ports, can I have one list with both?
    Last edited by Solarsonic; 05-18-2011 at 05:28 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I don't know of any lists that would take data types of int and String.
    Seems strange to have a list like:
    1, 2, "apple", 44, "house", "horse", 99, ...

  16. #16
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    I don't know of any lists that would take data types of int and String.
    Seems strange to have a list like:
    1, 2, "apple", 44, "house", "horse", 99, ...
    See, what would work here is a HashMap because there's two values that correspond with another, which is where I got the "parallel array lists", kind of like "parallel arrays" in that they're two objects that correspond with each other.

    And how you're saying it's strange, it doesn't seem strange to me to have host (string) and port (int) correspond with each other?

    But what would I do if there were 3 values that were corresponding? I couldn't use a HashMap then right?

    I could just use two ArrayLists: a String arraylist and an Integer arraylist, but as parallel arrays are generally discouraged I'm wondering if there's a better way of doing this.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How about a map?
    Java Code:
    Map<String, ArrayList<String>>
    The key is a String holding the host. The value is a List of Strings holding the ports.

  18. #18
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    How about a map?
    Java Code:
    Map<String, ArrayList<String>>
    The key is a String holding the host. The value is a List of Strings holding the ports.
    But what if I wanted 3 values corresponding with each other, instead of just two?
    Last edited by Solarsonic; 05-18-2011 at 05:50 AM.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Solarsonic View Post
    But what if I wanted 3 values corresponding with each other?
    What do you mean by that?

  20. #20
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    What do you mean by that?
    I'm just speaking theoretically here, because I'm interested.

    What if I wanted to create a "map" that stored a proxy's host, port, and type? So essentially 3 values corresponding to one another.

Page 1 of 2 12 LastLast

Similar Threads

  1. array lists in jsp and google api
    By gedas in forum New To Java
    Replies: 3
    Last Post: 04-07-2011, 03:18 PM
  2. Array Lists help!!
    By lilika in forum New To Java
    Replies: 12
    Last Post: 01-04-2011, 03:05 PM
  3. Array Lists and Club class
    By amekjian in forum New To Java
    Replies: 13
    Last Post: 11-03-2010, 01:42 AM
  4. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 10:47 PM
  5. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Posting Permissions

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