我正在使用 Apache POI 处理 Excel 文件。大多数时候它工作正常,但有时, getLastRowNum() 返回错误的数字,当我到达文件末尾时会导致空指针异常。
我想确保一切正常,即使 getLastRowNum() 出现问题也是如此。
for (int i = FIRST_ROW_TO_GET; i < sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
System.out.println(row.getCell(9));
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 if row.equals(null) 但它不会改变任何内容。
知道我能做什么吗?
如何查看 RecyclerView 顶部的最新项目?你能告诉我代码吗?
ImagesActivity.java
这是ImagesActivity我的项目的 Java 代码。您能告诉我如何RecyclerView以相反的顺序查看项目,即最新项目位于顶部吗?
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private ProgressBar mProgressCircle;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mProgressCircle = findViewById(R.id.progress_circle);
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用流从ResultSet中的int列中获取平均值,但是,我要做此工作的唯一方法是首先将数字放入列表中,然后在该列表上使用.stream()。
有没有更直接的方法可以做到这一点?
public void avgSalaryAbove150k(Connection con) {
String sqlQuery = "SELECT * FROM " + schemaName + "."+tableName+" WHERE EMPLOYEE_SALARY>150000";
List<Integer>salaries = new ArrayList<>();
try(
PreparedStatement preparedStatement = con.prepareStatement(sqlQuery);
ResultSet rs = preparedStatement.executeQuery();
){
while(rs.next()) {
int salary = rs.getInt("EMPLOYEE_SALARY");
salaries.add(salary);
}
System.out.println("Now showing average salary of employees with a salary of over 150,000...");
salaries.stream()
.mapToInt((x)->x)
.average()
.ifPresent(System.out::println);
System.out.println("");
}catch(SQLException ex) {
System.out.println("Failed to create the database connection.");
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
该方法目前可以从中使用,但是我有一种明显的感觉,这不是执行此操作的最佳方法。
我有多个类,每个类都有一个共同的领域。我正在编写一种处理提取此公共字段的方法。例如:
class A{
..
String blah;
..
}
class B{
..
String blah;
..
}
Run Code Online (Sandbox Code Playgroud)
因此,可以为每个类类型编写多个方法,而不是使用一个接受通用类的方法来实现?我想要的是一种类型的方法:
void func(List <T> data){
for(T t:data){
print t.blah()
}
}
Run Code Online (Sandbox Code Playgroud)
我知道上面的代码不正确,但是我们如何获得类似的功能?
我目前正在尝试将转换List<String>为List<Integer>带有流等。但是我不知道发生了什么。当它运行时,它给了我一些让我发狂的错误。
我已经尝试过(很少)从Java Streams中了解到的所有内容,也尝试过不使用它们,但是事实是我想以一种功能性的方式来实现它。我给我的方法(leeFichero)一个字符串f,它只是我的txt文件的路径。我想要的方法是返回一个List<Integer>带有值的a 。该文件的内容是这样的:
Run Code Online (Sandbox Code Playgroud)-3,-2,-1,0,1,2,3 1,3,5,7,9 100 2,4,6,8,10
public static List<Integer> leeFichero(String f) {
List<String>lineas=null;
try {
BufferedReader bf = new BufferedReader(new FileReader(f));
lineas = bf.lines().collect(Collectors.toList());
bf.close();
} catch (IOException e) {
System.out.println(e.toString());
}
List<Integer> intList = lineas.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
return intList;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它会显示以下错误消息:
Exception in thread "main" java.lang.NumberFormatException: For input string: "-3,-2,-1,0,1,2,3"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at ejercicios.ejercicio1.lambda$0(ejercicio1.java:108)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at …Run Code Online (Sandbox Code Playgroud) public class Solution1 {
public static void main(String[] args) {
System.out.println(Double.MAX_VALUE*2 == Double.POSITIVE_INFINITY);
System.out.println(Integer.MAX_VALUE+1 == Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE-1 == Integer.MAX_VALUE);
System.out.println(Double.MAX_VALUE+1 == Double.MAX_VALUE);
}
}
Run Code Online (Sandbox Code Playgroud)
对于上述内容,我们得到以下答复
true
false
true
true
Run Code Online (Sandbox Code Playgroud)
对于 Double.MAX_VALUE+1 == Double.MAX_VALUE 这是 true,但是为什么 Integer.MAX_VALUE+1 == Integer.MAX_VALUE 是 false?
为什么 Double.MAX_VALUE*2 == Double.POSITIVE_INFINITY 返回 true ?
谁能提供合理的理由和解释?
我浏览了 JavaDoc,但没有得到太多有用的信息。
我想知道我是否正确地写了这个equals方法.我的程序不断打印出"平等",即使两个对象Item和otherObject不相等.我有三个equals方法,一次运行一个似乎没有工作.三个等于方法正好位于彼此之后以供参考.我的主要是在最后.
import static java.lang.System.*;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
import java.math.*;
public class Item {
DecimalFormat df = new DecimalFormat("#.00");
//the properties of an Item
static double cash=59.00;
static double sum=0.00;
private int priority;
private String name;
private double price;
static boolean value= false;
//default constructer
public Item() {
priority = -1; //fill with default values
price = 0.00;
name = "No name yet";
}
public Item(int priority, String name, double price) {//constructor with all …Run Code Online (Sandbox Code Playgroud) 快速的问题.
为什么nextDouble可以接受
1 + ( 900 - 1) * random.nextDouble()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将其设置为
random.nextDouble((900 - 1) + 1) + 1);
Run Code Online (Sandbox Code Playgroud)
它告诉我nextDouble不适用于int类型
当然第一个是int,它接受了吗?
对不起,如果这是一个愚蠢的问题.
这是我的班级:
public class class1{
public static void main(String[] args) {
File source = new File("E:\\NUS_WID_Tags\\All_Tags.txt");
File target = new File("fiche1Filtered3.txt");
int i=0;
try {
Scanner s = new Scanner(source);
PrintStream psStream= new PrintStream(target);
while (s.hasNext()) {
System.out.println(i++);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
程序进入无限循环.
我写了一些与问题相关的代码,但我无法让它工作.输入两个数字后,程序陷入无限循环.这种方法有什么办法可行,还是完全错了?
import java.util.Scanner;
public class Arithmetic {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the numerator and denominator respectively : ");
double a = input.nextDouble();
double b = input.nextDouble();
double c;
for(c=0;;c+=0.000000000001){
if(b*c==a){
break;
}
}
System.out.print(c);
}
}
Run Code Online (Sandbox Code Playgroud) 请注意它是如何以合理的方式删除它们的.这就像它改变了数组中的所有内容,但这不仅仅是ArrayLists的作用吗?
public static void main(String[] args) {
// create a list of integer to Test our logic.
LinkedList<Integer> halfOf = new LinkedList<Integer>();
halfOf.add(1);
halfOf.add(2);
halfOf.add(3);
halfOf.add(4);
halfOf.add(5);
halfOf.add(6);
halfOf.add(7);
halfOf.add(8);
halfOf.remove(0);
halfOf.remove(1);
halfOf.remove(2);
System.out.println(halfOf);
Run Code Online (Sandbox Code Playgroud)
给出输出:
[2, 4, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud) import java.io.*;
import java.util.Scanner;
import java.lang.*;
class WordCount{
static String word;
public static void main(String args[])
{
int count=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the sentence");
word=in.nextLine();
for(int i=0;i<word.length();i++){
if(i!=word.length())
if(word.charAt(i)==' ' || word.charAt(i)!='.' && isNotSpace(word,i))
{
count++;
}
}
System.out.println("The number of words in the sentence are : " +count);
}
static boolean isNotSpace(String word,int i)
{
if(word.charAt[i+1]!=' ')
return true;
else
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我声明了一个名为word的静态变量,并通过从main方法传递单词变量来调用"isNotSpace"方法.但是我在"isNotSpace"方法中遇到错误:
WordCount.java:23: error: cannot find symbol
if(word.charAt[i+1]!=' ')
^
symbol: variable charAt
location: variable …Run Code Online (Sandbox Code Playgroud) java ×12
java-stream ×2
android ×1
apache-poi ×1
equals ×1
generics ×1
integer ×1
linked-list ×1
list ×1
methods ×1
random ×1