Results 1 to 3 of 3
  1. #1
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Better alternative for ArrayList

    Java Code:
    public static void format(List<Integer> list) {
    		int i = 0;
    		StringBuilder str = new StringBuilder();
    		while (i < list.size() - 1) {
    			if (i != 0)
    				str.append(",");
    			str.append("" + list.get(i));
    			for (int j = i; j <= list.size() - 1; j++) {
    				if (j != list.size() - 1)
    					if ((list.get(j) + 1) == list.get(j + 1)) {
    						continue;
    					}
    				if (i == j) {
    					i++;
    					break;
    				} else {
    					i = j + 1;
    					str.append("-"
    							+ String.format("%02d", ((list.get(j)) % 100)));
    					break;
    				}
    			}
    		}
    		System.out.println(str);
    	}
    
    	public static void main(String args[]) {
    		List<Integer> years = new ArrayList<Integer>();
    		years.add(2000);
    		years.add(2001);
    		years.add(2002);
    		years.add(2004);
    		years.add(2006);
    		years.add(2007);
    		years.add(2008);
    		format(years);
    	}

    OUTPUT : 2000-02,2004,2006-08

    Is there any better alternative for ArrayList, so that i could reduce the logic here ?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Better alternative for ArrayList

    Rather then figuring out what your code does, why not tell me what the goal is?

  3. #3
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Better alternative for ArrayList

    Quote Originally Posted by sunde887 View Post
    Rather then figuring out what your code does, why not tell me what the goal is?
    I would get years as user input, which i would store in ArrayList (as of code). If there are consecutive years, make the output as 2001-03
    just like this one : 2000-02,2004,2006-08

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Which would be the most effecient alternative?
    By Krystah in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 10:56 PM
  3. Alternative to JNLP
    By tecno40 in forum Java Applets
    Replies: 0
    Last Post: 12-22-2010, 12:38 AM
  4. Is there an alternative to OBEX?
    By danielpereira in forum Advanced Java
    Replies: 0
    Last Post: 10-17-2010, 11:36 PM
  5. Looking for alternative to Web Start
    By SamSam in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2009, 07:19 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
  •