मेरे उदाहरण में सरणी शामिल चरित्र स्ट्रिंग अलग लंबाई के साथ और है कि मैं व्यवस्था करने के लिए स्ट्रिंग और विभिन्न सरणियों के अन्य तार अ- थे असमर्थ था की वजह से की कंसोल पर मैच। एक अलग अवधारणा के साथ मैं कंसोल पर उन सरणी को व्यवस्थित कर सकता हूं, जो मेरे कोड नीचे दिए गए हैं।
package arrayformat;
/**
*
* @author Sunil
*/
public class ArrayFormat {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] productId = new int[]
{1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,};
String[] productName= new String[]{"Pepsi","kissan jam","Herbal
oil","Garnier man's","Lays chips","biscuits","Bournvita","Cadbury","Parker
Vector","Nescafe",};
String[] productType = new String[]{"Cold Drink","Jam","Oil","Face
wash","chips","Biscuits","Health
Supplement","Chocolate","Stationary","Coffee",};
float[] productPrice = new float[]{24,65,30,79,10,20,140,20,150,80,};
int productNameMaxlength=0;
int productTypeMaxlength=0;
for (String productName1 : productName) {
if (productNameMaxlength < productName1.length()) {
productNameMaxlength = productName1.length();
}
}
for (String productType1 : productType) {
if (productTypeMaxlength < productType1.length()) {
productTypeMaxlength = productType1.length();
}
}
for(int i=0;i<productType.length;i++)
{
System.out.print(i);
System.out.print("\t");
System.out.print(productId[i]);
System.out.print("\t");
System.out.print(productName[i]);
for(int j=0;j<=productNameMaxlength-productName[i].length
();j++)
{
System.out.print(" ");
}
System.out.print("\t");
System.out.print(productType[i]);
for(int j=0;j<=productTypeMaxlength-productType[i].length
();j++)
{
System.out.print(" ");
}
System.out.print("\t");
System.out.println(productPrice[i]);
}
}
}
and output is--
Sr.No ID NAME TYPE PRICE
0 1001 Cadbury Chocolate 20.0
1 1002 Parker Vector Stationary 150.0
2 1003 Nescafe Coffee 80.0
3 1004 kissan jam Jam 65.0
4 1005 Herbal oil Oil 30.0
5 1006 Garnier man's Face wash 79.0
6 1007 Lays chips chips 10.0
7 1008 biscuits Biscuits 20.0
8 1009 Bournvita Health Supplement 140.0
9 1010 Pepsi Cold Drink 24.0
जब से मैं मेरे सवाल का मैं कहाँ ब्लॉक की वजह से मेरे सवाल पूछना सवाल पूछने और जवाब मैं अपने जवाब के हवाले कर रहा हूँ और इस सरणी प्रारूप एक अलग तरह मुझे लगता था के लिए है जवाब देने में असमर्थ हूँ।
'String.Format ("% 4s ", ...) ' – khachik
@khachik: क्या आप कृपया वर्णन कर सकते हैं कि 'String.format (% 4s", ...)' काम करता है? और 'प्रारूप() ' –