list.foreach(system.out::print)如何使用tostring
时间: 2023-06-13 13:05:32 浏览: 143
如果你想在 `foreach` 方法中使用 `toString` 方法,你可以使用 lambda 表达式来实现:
```
list.forEach(item -> System.out.print(item.toString()));
```
或者你可以使用方法引用,但需要调用 `toString` 方法:
```
list.forEach(System.out::print);
```
在这种情况下,`System.out.print` 方法将自动调用每个元素的 `toString` 方法,因为 `println` 方法需要将每个元素转换为字符串。
相关问题
将以下代码转成python的代码 private static void fileRename(String newFileName, String folder) { File file = new File(folder); System.out.println("Reading this " + file.toString()); if (file.isDirectory()) { File[] files = file.listFiles(); List<File> filelist = Arrays.asList(files); filelist.forEach(f -> { System.out.println(f.getAbsolutePath()); String newName = folder + newFileName; System.out.println(newName); boolean isRenamed = f.renameTo(new File(newName)); if (isRenamed) System.out.println(String.format("Renamed this file %s to %s", f.getName(), newName)); else System.out.println(String.format("%s file is not renamed to %s", f.getName(), newName)); }); }
```python
import os
def file_rename(new_file_name: str, folder: str):
file = os.path.join(folder)
print("Reading this " + file)
if os.path.isdir(file):
files = os.listdir(file)
for f in files:
print(os.path.abspath(f))
new_name = os.path.join(folder, new_file_name)
print(new_name)
is_renamed = os.rename(f, new_name)
if is_renamed:
print(f"Renamed this file {f} to {new_name}")
else:
print(f"{f} file is not renamed to {new_name}")
```
阅读全文