下面哪个赋值语句是错误的?( ) A. int i = (short)3; B. int i = new Integer(5).intValue(); C. int i = (integer)3; D. int i = (int)3.14;
时间: 2024-02-21 07:58:08 浏览: 43
赋值语句 C 是错误的。
在语句 A 中,将 `short` 类型的值 `3` 强制转换为 `int` 类型,这是一个合法的类型转换,因此语句 A 是正确的。
在语句 B 中,将 `Integer` 对象的值转换为 `int` 类型,可以通过调用 `intValue()` 方法来实现,因此语句 B 是正确的。
在语句 C 中,将 `3` 强制转换为 `integer` 类型,这是一个错误的类型转换。实际上,`integer` 不是 Java 中的基本数据类型,而是 `Integer` 的一个别名,因此应该将 `3` 强制转换为 `Integer` 类型。因此,语句 C 应该改为:
```java
Integer i = (Integer)3;
```
在语句 D 中,将 `3.14` 强制转换为 `int` 类型,会将小数部分截断,得到整数值 `3`。因此,语句 D 是正确的。
因此,答案是 C。
相关问题
解读int &pa=a, &pb=b;
int是一种数据类型,表示整数(integer)。在计算机编程中,int通常表示32位整数,范围为-2,147,483,648到2,147,483,647。在某些编程语言中,还有其他类型的整数,如short int和long int,它们位数不同,因此它们能表示的范围也不同。在C/C++语言中,int是有符号整数,可以表示正数、负数和0。在Java语言中,int也是有符号整数,但其范围为-2,147,483,648到2,147,483,647。在Python语言中,整数类型int没有位数限制,可以表示任意大的整数。
在下列方法上新增功能,第四列,第五列,第六列的合并行数以第三列的合并行数为准 public static int exportToExcelForXlsx(List<List> objData, String sheetName, List<String> columns, List mergeIndex, HttpServletResponse response) { int flag = 0; XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet1 = wb.createSheet(sheetName); sheet1row1.setHeight((short) 520); if (columns != null && columns.size() > 0) { for(int i=0;i<columns.size();i++) { String column = columns.get(i); //列 XSSFCell cell = sheet1row1.createCell(i); cell.setCellValue(column); } } int dataSatrtIndex = 1; boolean isMerge = false; if(mergeIndex != null && mergeIndex.size() != 0) { isMerge = true; } if (objData != null && objData.size() > 0) { Map<Integer, PoiModel> poiModels = new HashMap<Integer, PoiModel>(); int i = 0; for ( ; i < objData.size(); i++) { XSSFRow row = sheet1.createRow((short) (i+dataSatrtIndex)); List<Object> list = objData.get(i); DecimalFormat decimalFormat = new DecimalFormat("0.00"); int j = 0; for (Object o : list) { String content = ""; if(o != null) { if (o.toString().contains(".") && isNumeric(o.to }else { XSSFCell cell = row.createCell(j); cell.setCellValue(content); } j++; } } if(poiModels != null && poiModels.size() != 0) { for(Integer key : poiModels.keySet()) { PoiModel poiModel = poiModels.get(key); XSSFRow lastRow = sheet1.getRow(poiModel.getRowIndex()); XSSFCell lastCell = lastRow.getCell(poiModel.getCellIndex()); lastCell.setCellValue(poiModel.getContent()); lastCell.setCellStyle(style); lastRow.setRowStyle(style); if(poiModel.getRowIndex() != i + dataSatrtIndex -1) { sheet1.addMergedRegion(new CellRangeAddress(poiModel.getRowIndex(), i + dataSatrtIndex - 1, poiModel.getCellIndex(), poiModel.getCellIndex())); } } } } else { flag = -1; }
可以考虑新增以下功能:
1. 合并单元格:根据传入的合并行数,将指定列的单元格进行合并。可以使用XSSFCellStyle的setAlignment()方法,将内容对齐方式设置为居中。
示例代码:
```java
// 获取合并列数
int mergeCount = Integer.parseInt(mergeIndex.get(2).toString());
// 合并单元格
for (int i = 0; i < mergeCount; i++) {
String startCell = mergeIndex.get(0) + (Integer.parseInt(mergeIndex.get(1).toString()) + i);
String endCell = mergeIndex.get(0) + (Integer.parseInt(mergeIndex.get(1).toString()) + mergeCount - 1);
CellRangeAddress cellRange = new CellRangeAddress(CellReference.convertColStringToIndex(startCell.substring(0, 1)), Integer.parseInt(startCell.substring(1)) - 1,
CellReference.convertColStringToIndex(endCell.substring(0, 1)), Integer.parseInt(endCell.substring(1)) - 1);
sheet1.addMergedRegion(cellRange);
// 设置对齐方式
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
cellRange.setCellStyle(style);
}
```
2. 设置表头样式:可以使用XSSFCellStyle的setFont()方法和setFillForegroundColor()方法,分别设置字体和背景颜色。
示例代码:
```java
// 设置表头样式
XSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setBold(true);
headerStyle.setFont(font);
// 设置表头单元格样式
for (int i = 0; i < columns.size(); i++) {
sheet1row1.getCell(i).setCellStyle(headerStyle);
}
```
3. 设置列宽:可以使用setColumnWidth()方法设置指定列的宽度。
示例代码:
```java
// 设置列宽
for (int i = 0; i < columns.size(); i++) {
sheet1.setColumnWidth(i, 5000);
}
```
完整代码如下:
```java
public static int exportToExcelForXlsx(List<List> objData, String sheetName, List<String> columns, List mergeIndex, HttpServletResponse response) {
int flag = 0;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet1 = wb.createSheet(sheetName);
// 创建表头
XSSFRow sheet1row1 = sheet1.createRow(0);
for (int i = 0; i < columns.size(); i++) {
sheet1row1.createCell(i).setCellValue(columns.get(i));
}
// 设置表头样式
XSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setBold(true);
headerStyle.setFont(font);
// 设置表头单元格样式
for (int i = 0; i < columns.size(); i++) {
sheet1row1.getCell(i).setCellStyle(headerStyle);
}
// 填充数据
for (int i = 0; i < objData.size(); i++) {
XSSFRow row = sheet1.createRow(i + 1);
for (int j = 0; j < objData.get(i).size(); j++) {
row.createCell(j).setCellValue(objData.get(i).get(j).toString());
}
}
// 设置列宽
for (int i = 0; i < columns.size(); i++) {
sheet1.setColumnWidth(i, 5000);
}
// 合并单元格
int mergeCount = Integer.parseInt(mergeIndex.get(2).toString());
for (int i = 0; i < mergeCount; i++) {
String startCell = mergeIndex.get(0) + (Integer.parseInt(mergeIndex.get(1).toString()) + i);
String endCell = mergeIndex.get(0) + (Integer.parseInt(mergeIndex.get(1).toString()) + mergeCount - 1);
CellRangeAddress cellRange = new CellRangeAddress(CellReference.convertColStringToIndex(startCell.substring(0, 1)), Integer.parseInt(startCell.substring(1)) - 1,
CellReference.convertColStringToIndex(endCell.substring(0, 1)), Integer.parseInt(endCell.substring(1)) - 1);
sheet1.addMergedRegion(cellRange);
// 设置对齐方式
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
cellRange.setCellStyle(style);
}
// 导出excel文件
try {
OutputStream out = response.getOutputStream();
String fileName = sheetName + ".xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
wb.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
flag = 1;
}
return flag;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)