5.22 int配列を文字列につなぐ例

833 ワード

/*
 *   :                      
 *   :
 * 		int[] arr = {1,2,3};	
 *     :
 *		"[1, 2, 3]"
 *   :
 * 		A:         ,       
 * 		B:         "["
 * 		C:  int  ,       
 * 		D:             
 * 			 :        "]"
 * 			  :            
 * 		E:         
 */
public class StringTest {
	public static void main(String[] args) {
		//          
		int[] arr = { 1, 2, 3 };

		//          ,       
		String s = "";

		//          "["
		s += "[";

		//   int  ,       
		for (int x = 0; x < arr.length; x++) {
			//              
			if (x == arr.length - 1) {
				//         "]"
				s += arr[x];
				s += "]";
			} else {
				//             
				s += arr[x];
				s += ", ";
			}
		}

		//          
		System.out.println("       :" + s);
	}
}