Results 1 to 9 of 9
  1. #1
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default Put String Arrays inside an Arraylist (or anyother list)

    Hi Im trying to place some string arrays inside an array list. How do I do that??

    here is the code I have got so far

    Java Code:
    ArrayList Times= new ArrayList();
    String tues="11:00";
    String mon="10:00";
    String[] days = {Mon,tues};
    
    Time.add(?? how to I put days here ??);

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You mean you want to add your string such that it says "10:00,11:00"? Or do you want to insert both objects separately? What exactly is/are the string(s) in this example that you want to add to the object?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    uff...
    Mon != mon
    Time != Times

    correct:
    Java Code:
    		ArrayList Times= new ArrayList();
    		String tues="11:00";
    		String mon="10:00";
    		String[] days = {mon,tues};
    		Times.add(days);
    but,
    Java Code:
    		List<String[]> times= new ArrayList<String[]>();
    		String tues="11:00";
    		String mon="10:00";
    		String[] days = {mon,tues};
    		times.add(days);
    is much better ;-)

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by eRaaaa View Post
    Java Code:
    		List<String[]> times= new ArrayList<String[]>();
    		String tues="11:00";
    		String mon="10:00";
    		String[] days = {mon,tues};
    		times.add(days);
    That code makes a list of String[]s which I'm not sure is what the OP is after.

  5. #5
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Im sorry everyone I made a mistake I placed my } before my

    Times.add(days);

    which is why it wouldnt work. Thanks anyway

  6. #6
    Toirniuil is offline Member
    Join Date
    Sep 2010
    Posts
    8
    Rep Power
    0

    Default

    Why is the List<String[]> times= new ArrayList<String[]>(); better??

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Zack View Post
    That code makes a list of String[]s which I'm not sure is what the OP is after.
    yes I agree with you, a class would be better but that is what Toirniuil wanted :)

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by Toirniuil View Post
    Why is the List<String[]> times= new ArrayList<String[]>(); better??
    Look up interfaces, and how much they help with code maintenance.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Nitpick: Arrays and Generics don't mix well. I would suggest a List of Lists.
    Java Code:
    List<List<String>> times = new ArrayList<List<String>>();
    List<String> days = new ArrayList<String>();
    days.add("11:00");
    days.add("10:00");
    times.add(days);
    db

Similar Threads

  1. Retrieve array inside a list as a Map value
    By Ms.Ranjan in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2010, 01:30 PM
  2. how to read the ArrayList inside HashMap
    By koddy in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 02:41 PM
  3. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  4. Replies: 3
    Last Post: 11-21-2009, 06:48 PM
  5. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 01:23 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
  •