求助 关于字符串的二维数组
我知道字符串本身就是一个字符数组但是有没有可能实现一个二维数组 里的元素是字符串?
不是转化成二维数组 而是将字符串存入2维数组
求助!
----------------解决方案--------------------------------------------------------
当然可以 在数组里保存字符串
String[][] 就可以保存字符串了,
----------------解决方案--------------------------------------------------------
回复 2楼 hhwz
但是怎么将字符串存入其中import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class testExcelReader {
public static void main(String[] args) throws BiffException, IOException {
String excelFile="E:\\abc.xls";
Workbook rwb = null;
String[][] str = null;
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//获取第一行,第一列的值
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
Cell c00 = sheet.getCell(i, j);
String strc00 = c00.getContents();
str[i][j]=strc00;
}
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
System.out.println(str[i][j]);
}
}
}
出错了
----------------解决方案--------------------------------------------------------
程序代码:
String[][] str = new String[][]{{}};
String [] s = new String[]{"第一个字符串","第二个字字符串"};
str[0]=s; //给第一层赋值一个数组
for(int i=0;i<str.length;i++){
for(int j=0;j<str[i].length;j++){
System.out.println(str[i][j]);
}
}
String [] s = new String[]{"第一个字符串","第二个字字符串"};
str[0]=s; //给第一层赋值一个数组
for(int i=0;i<str.length;i++){
for(int j=0;j<str[i].length;j++){
System.out.println(str[i][j]);
}
}
----------------解决方案--------------------------------------------------------
回复 4楼 hhwz
版主大大 还是出错啊程序代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class testExcelReader {
public static void main(String[] args) throws BiffException, IOException {
String excelFile="E:\\abc.xls";
Workbook rwb = null;
String[][] str =new String[][]{{}};
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//获取第一行,第一列的值
for(int i=0;i<10;i++){
String[] s=new String[]{};
for(int j=0;j<10;j++){
Cell c00 = sheet.getCell(i, j);
String strc00 = c00.getContents();
s[j]=strc00; //这是第28行
}
str[i]=s;
}
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
System.out.println(str[i][j]);
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class testExcelReader {
public static void main(String[] args) throws BiffException, IOException {
String excelFile="E:\\abc.xls";
Workbook rwb = null;
String[][] str =new String[][]{{}};
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//获取第一行,第一列的值
for(int i=0;i<10;i++){
String[] s=new String[]{};
for(int j=0;j<10;j++){
Cell c00 = sheet.getCell(i, j);
String strc00 = c00.getContents();
s[j]=strc00; //这是第28行
}
str[i]=s;
}
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
System.out.println(str[i][j]);
}
}
}
at testExcelReader.main(testExcelReader.java:28)
----------------解决方案--------------------------------------------------------
因为数组是需要一个长度的 在不知道的情况下是会出错的
如果非要用的话 那么就要指明最大长度 不然还是有下标溢出的错误
String excelFile="E:\\abc.xls";
Workbook rwb = null;
String[][] str =new String[10][10];
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//获取第一行,第一列的值
for(int i=0;i<10;i++){
StringBuffer strc00 = new StringBuffer();
for(int j=0;j<10;j++){
Cell c00 = sheet.getCell(i, j);
strc00.append(c00.getContents()).append(" ");
}
str[i]= strc00.toString().split(" ")
}
for(int i=0;i<10;i++)
for(int j=0;j<10;j++){
System.out.println(str[i][j]);
}
}
[ 本帖最后由 hhwz 于 2013-1-3 19:50 编辑 ]
----------------解决方案--------------------------------------------------------
懂了
终于有用了 谢谢斑竹
----------------解决方案--------------------------------------------------------
肯定可以
----------------解决方案--------------------------------------------------------