What's the simplest way to print a Java array?
(32个答案)
4个月前关闭。
我试图弄清楚如何为我正在处理的项目打印ArrayList
import java.util.*;
import java.nio.*;
public class TestBinomialUtils {
public static void main(String args[]) {
TextUtils wordMaker = new TextUtils();
GridGroups var = new GridGroups();
ArrayList strArrayList = wordMaker.buildRandomGridData(var);
int index = strArrayList.size()-1;
List<Integer> dataTypes = strArrayList.subList(index, index);
for (int i = 0; i < strArrayList.size()-1; i++) {
System.out.println(strArrayList.get(i));
}
}
}
class NumSysConv {
//convert to radix method
public String convert(String Nmbr, int Base, int BaseConv) {
String ConvNmbr = "";
String NewNmbr = "";
NewNmbr += Convert2Base10(Nmbr, Base);
ConvNmbr += Convert2Base(NewNmbr, BaseConv);
return ConvNmbr;
}
private String Convert2Base(String Nmbr, int Base) {
String NewNmbr = "";
char Hex = '0';
int cnt = 0;
int NmbrInteger = 0;
int pwr = Nmbr.length() - 1;
while (cnt < Nmbr.length()) {
switch (Nmbr.charAt(cnt)) {
case '1':
NmbrInteger += 1 * (int) (Math.pow(10, pwr));
break;
case '2':
NmbrInteger += 2 * (int) (Math.pow(10, pwr));
break;
case '3':
NmbrInteger += 3 * (int) (Math.pow(10, pwr));
break;
case '4':
NmbrInteger += 4 * (int) (Math.pow(10, pwr));
break;
case '5':
NmbrInteger += 5 * (int) (Math.pow(10, pwr));
break;
case '6':
NmbrInteger += 6 * (int) (Math.pow(10, pwr));
break;
case '7':
NmbrInteger += 7 * (int) (Math.pow(10, pwr));
break;
case '8':
NmbrInteger += 8 * (int) (Math.pow(10, pwr));
break;
case '9':
NmbrInteger += 9 * (int) (Math.pow(10, pwr));
break;
}
cnt++;
pwr--;
}
while (NmbrInteger > 0) {
int NmbrDivide = NmbrInteger/Base;
int NmbrRemainder = NmbrInteger%Base;
NmbrInteger = NmbrDivide;
switch(NmbrRemainder) {
case 10:
Hex = 'A';
break;
case 11:
Hex = 'B';
break;
case 12:
Hex = 'C';
break;
case 13:
Hex = 'D';
break;
case 14:
Hex = 'E';
break;
case 15:
Hex = 'F';
break;
case 16:
Hex = 'G';
break;
case 17:
Hex = 'H';
break;
case 18:
Hex = 'I';
break;
case 19:
Hex = 'J';
break;
case 20:
Hex = 'K';
break;
case 21:
Hex = 'L';
break;
case 22:
Hex = 'M';
break;
case 23:
Hex = 'N';
break;
case 24:
Hex = 'O';
break;
case 25:
Hex = 'P';
break;
case 26:
Hex = 'Q';
break;
case 27:
Hex = 'R';
break;
case 28:
Hex = 'S';
break;
case 29:
Hex = 'T';
break;
case 30:
Hex = 'U';
break;
case 31:
Hex = 'V';
break;
case 32:
Hex = 'W';
break;
case 33:
Hex = 'X';
break;
case 34:
Hex = 'Y';
break;
case 35:
Hex = 'Z';
break;
}
if (NmbrRemainder > 9) {
NewNmbr = Hex + NewNmbr;
} else {
NewNmbr = NmbrRemainder + NewNmbr;
}
}
return NewNmbr;
}
private String Convert2Base10(String Nmbr, int Base) {
int NewNmbr = 0;
int cnt = 0;
int pwr = Nmbr.length() - 1;
int Value = 0;
while (cnt < Nmbr.length()) {
switch (Nmbr.charAt(cnt)) {
case '0':
Value = 0;
break;
case '1':
Value = 1;
break;
case '2':
Value = 2;
break;
case '3':
Value = 3;
break;
case '4':
Value = 4;
break;
case '5':
Value = 5;
break;
case '6':
Value = 6;
break;
case '7':
Value = 7;
break;
case '8':
Value = 8;
break;
case '9':
Value = 9;
break;
case 'A':
Value = 10;
break;
case 'B':
Value = 11;
break;
case 'C':
Value = 12;
break;
case 'D':
Value = 13;
break;
case 'E':
Value = 14;
break;
case 'F':
Value = 15;
break;
case 'G':
Value = 16;
break;
case 'H':
Value = 17;
break;
case 'I':
Value = 18;
break;
case 'J':
Value = 19;
break;
case 'K':
Value = 20;
break;
case 'L':
Value = 21;
break;
case 'M':
Value = 22;
break;
case 'N':
Value = 23;
break;
case 'O':
Value = 24;
break;
case 'P':
Value = 25;
break;
case 'Q':
Value = 26;
break;
case 'R':
Value = 27;
break;
case 'S':
Value = 28;
break;
case 'T':
Value = 29;
break;
case 'U':
Value = 30;
break;
case 'V':
Value = 31;
break;
case 'W':
Value = 32;
break;
case 'X':
Value = 33;
break;
case 'Y':
Value = 34;
break;
case 'Z':
Value = 35;
break;
}
NewNmbr += (int) (Value * Math.pow(Base, pwr));
pwr--;
cnt++;
}
String FinalNmbr = "";
return FinalNmbr + NewNmbr;
}
}
class TextUtils {
public static final int UPPER_CASE = 1;
public static final int LOWER_CASE = 2;
public static final int MIXED_CASE = 3;
public static final int NUMBER_CASE = 4;
public static final int SPECIAL_CASE = 5;
public int rndmNmbrUpr = 1000;
public int rndmNmbrLwr = 1;
public String makeWords(int wordCnt, int caseStyle, int lwrWordSize, int uprWordSize) {
NumSysConv conv = new NumSysConv();
String words = "";
int base[] = {2, 8, 10, 16};
for (int i = 0; i < wordCnt; i++) {
int rndmSize = (int) (Math.random()*(uprWordSize-lwrWordSize+1)+lwrWordSize);
for (int j = 0; j < rndmSize; j++) {
switch(caseStyle) {
case UPPER_CASE:
char rndmUprChar = (char) (Math.random()*('Z'-'A'+1)+'A');
words += rndmUprChar;
break;
case LOWER_CASE:
char rndmLwrChar = (char) (Math.random()*('z'-'a'+1)+'a');
words += rndmLwrChar;
break;
case MIXED_CASE:
char rndmMixedChar;
int rndmCase = (int) (Math.random()*(2-1+1)+1);
if (rndmCase == 1) {
rndmMixedChar = (char) (Math.random()*('Z'-'A'+1)+'A');
words += rndmMixedChar;
} else if (rndmCase == 2) {
rndmMixedChar = (char) (Math.random()*('z'-'a'+1)+'a');
words += rndmMixedChar;
}
break;
case NUMBER_CASE:
int baseInt = (int) (Math.random()*(base.length));
int rndmNmbrInt = (int) (Math.random()*(rndmNmbrUpr-rndmNmbrLwr+1)+rndmNmbrLwr);
String rndmNmbr = "" + rndmNmbrInt;
String rndmNmbrConv = conv.convert(rndmNmbr, 10, base[baseInt]);
words+= rndmNmbrConv;
break;
case SPECIAL_CASE:
int rndmSpecialCase = (int) (Math.random()*(4-1+1)+1);
char rndmSpecialChar;
switch(rndmSpecialCase) {
case 1:
rndmSpecialChar = (char) (Math.random()*('/'-'!'+1)+'!');
words += rndmSpecialChar;
break;
case 2:
rndmSpecialChar = (char) (Math.random()*('@'-':'+1)+':');
words += rndmSpecialChar;
break;
case 3:
rndmSpecialChar = (char) (Math.random()*('`'-'['+1)+'[');
words += rndmSpecialChar;
break;
case 4:
rndmSpecialChar = (char) (Math.random()*('~'-'{'+1)+'{');
words += rndmSpecialChar;
break;
}
break;
}
}
if (i == wordCnt - 1) {
return words;
} else {
words += " ";
}
}
return words;
}
public String[] wrapText(String str, int width) {
String strArray[] = str.split(" ");
int lengthCnt = 0;
ArrayList<String> returnStrArray = new ArrayList<String>();
String str2 = "|";
for (int i = 0; i < strArray.length; i++) {
lengthCnt += strArray[i].length();
if (lengthCnt >= width) {
lengthCnt -= strArray[i].length();
if (lengthCnt == width) {
str2 += "|";
returnStrArray.add(str2);
str2 = "|" + strArray[i] + " ";
} else {
while (lengthCnt <= width) {
if (lengthCnt == width) {
str2 += "|";
returnStrArray.add(str2);
str2 = "|" + strArray[i] + " ";
break;
} else {
str2 += " ";
lengthCnt++;
}
}
}
lengthCnt = strArray[i].length() + 1;
} else {
str2 += strArray[i];
str2 += " ";
lengthCnt++;
}
if (i + 1 == strArray.length) {
while (lengthCnt <= width) {
if (lengthCnt == width) {
str2 += "|";
returnStrArray.add(str2);
break;
} else {
str2 += " ";
lengthCnt++;
}
}
}
}
String[] returnStr = new String[returnStrArray.size()];
for (int i = 0; i < returnStr.length; i++) {
returnStr[i] = returnStrArray.get(i);
}
return returnStr;
}
public String[] trimStrings(String[] strArray) {
String[] returnStrArray = new String[strArray.length];
for (int i = 0; i < returnStrArray.length; i++) {
returnStrArray[i] = "";
for (int j = 0; j < strArray[i].length(); j++) {
if (strArray[i].charAt(j) == ' ') {
if (strArray[i].charAt(j+1) == ' ' || strArray[i].charAt(j+1) == '|') {
returnStrArray[i] += "|";
j = strArray[i].length();
} else {
returnStrArray[i] += strArray[i].charAt(j);
}
} else {
returnStrArray[i] += strArray[i].charAt(j);
}
}
}
return returnStrArray;
}
public ArrayList buildRandomGridData(GridGroups var) {
ArrayList returnArray = new ArrayList();
int rowCnt = (int) (Math.random()*(var.rowCntUpr-var.rowCntLwr+1)+var.rowCntLwr);
int wordCnt = (int) (Math.random()*(var.wordCntUpr-var.wordCntLwr+1)+var.wordCntLwr);
int wrapLen = (int) (Math.random()*(var.wrapLenUpr-var.wrapLenLwr+1)+var.wrapLenLwr);
var.wordStyle = (int) (Math.random()*(SPECIAL_CASE-UPPER_CASE+1)+UPPER_CASE);
ArrayList<Integer> titleIndex = new ArrayList<Integer>();
for (int i = 0; i < rowCnt; i++) {
//var.rowType = ;
if (var.getRowType() == false) {
String words = makeWords(wordCnt, var.wordStyle, var.wordSizeLwr, var.wordSizeUpr);
String[] wrap = wrapText(words, wrapLen);
returnArray.add(i, wrap);
} else if (var.rowType == true) {
String words = makeWords(wordCnt, var.wordStyle, var.wordSizeLwr, var.wordSizeUpr);
String[] wrap = wrapText(words, wrapLen);
returnArray.add(i, wrap);
titleIndex.add(i);
}
}
returnArray.add(titleIndex.toArray());
return returnArray;
}
}
class GridGroups {
public boolean rowType = false;
public int wordStyle = 1;
public int nmbrBase = 10;
public int rowCntLwr = 1;
public int rowCntUpr = 10;
public int wordSizeLwr = 1;
public int wordSizeUpr = 8;
public int wordCntLwr = 2;
public int wordCntUpr = 10;
public int wrapLenLwr = 8;
public int wrapLenUpr = 15;
}
当我打印出对象时,得到以下信息
[Ljava.lang.String; @ 61443d8f
[Ljava.lang.String; @ 445b84c0
(这只是一个例子,但您可以理解)
我试图弄清楚如何在ArrayList中打印对象的String表示形式。
(如果它像使用toString()一样简单,那么您如何正确使用它,因为我已经尝试过并导致错误)
谁能帮我?
我是一名优秀的程序员,十分优秀!