본문 바로가기

Dev

Print multiple values in a row by using ArrayList.subList

728x90


    import java.util.ArrayList;
    import java.util.List;
    
    public class SujeongTest {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SujeongTest sujeongTest = new SujeongTest();
    
    		
    		List<String> stringList = sujeongTest.makeStringList();
    		String printValue = "";
    		int listSize = stringList.size();
    		
    		int countRow = 1;
    		int sIdx = 0;
    		int eIdx = sujeongTest.getLastIndex( countRow ,   listSize) ;
    		while( sIdx < listSize ){
    			for(String string : stringList.subList(sIdx, eIdx)){
    				printValue = printValue.concat(string).concat("    ");
    			}
    			printValue = printValue.trim().concat("\n");
    			sIdx = eIdx;
    			eIdx = sujeongTest.getLastIndex(++countRow ,  listSize);
    		}
    		
    		System.out.println(printValue);
    	}
    
    
    	private int getLastIndex(int countRow , int listSize) {
    		int countColumn = 4;
    		int endIndex =(countRow * countColumn); 
    		if(listSize <= endIndex) {
    			endIndex = listSize;
    		}
    		return endIndex;
    	}
    
    	
    	private List<String> makeStringList(){
    		List<String> stringList = new ArrayList<String>();
    		stringList.add("1.AAAAA");
    		stringList.add("2.BBBBB");
    		stringList.add("3.CCCCC");
    		stringList.add("4.DDDDD");
    		stringList.add("5.EEEEE");
    		stringList.add("6.FFFFF");
    		stringList.add("7.GGGGG");
    		stringList.add("8.HHHHH");
    		stringList.add("9.IIIII");
    		stringList.add("10.JJJJJ");
    		stringList.add("11.KKKKK");
    		
    		return stringList;
    	}
    }