有什么办法可以避免get(0)
在列表迭代中使用吗?
get(0)
迭代列表时使用它总是有风险的.
我确信在这个列表中我只有一个对象.
(PS我记得我的上一位经理总是对我说要避免get(0)
在列表迭代中使用.)
我正在尝试使用JDBC连接到我的locahost上的Derby数据库.
我使用命令启动了数据库:java -jar lib;derbyrun.jar server start
,它在端口1527上成功启动.
在另一个命令终端上,我使用命令:java -classpath .;lib;derbyclient.jar testsqldatabase.TestSQLDatabase
但是我收到以下错误:
java.sql.SQLException: No suitable driver found for jdbc:postgresql:COREJAVA
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at testsqldatabase.TestSQLDatabase.getConnection(TestSQLDatabase.jav
)
at testsqldatabase.TestSQLDatabase.runTest(TestSQLDatabase.java:39)
at testsqldatabase.TestSQLDatabase.main(TestSQLDatabase.java:26)
Run Code Online (Sandbox Code Playgroud)
我的datatbase.properties文件包含以下行:
jdbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA
jdbc.username=dbuser
jdbc.password=secret
Run Code Online (Sandbox Code Playgroud)
java程序如下:
public class TestSQLDatabase
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
try
{
runTest();
}
catch(SQLException ex)
{
for(Throwable t: ex)
t.printStackTrace();
}
}
/*Runs a test by …
Run Code Online (Sandbox Code Playgroud) 我遇到了以下代码,想知道这是否是好的做法:
LinkedList<String> ll = new LinkedList();
Run Code Online (Sandbox Code Playgroud)
我会写
List<String> l = new LinkedList<String> ();
Run Code Online (Sandbox Code Playgroud)
所以这里有两个问题:
List
而不是ArrayList
/LinkedList
。在我的代码中创建搜索方法以搜索字符串时,我一直收到此错误.我已经经历了很多试图解决这个问题的例子,但我找不到.感谢您的帮助,并建议您给予.
public class runNote {
public static void main(String[] args) {
// TODO Auto-generated method stub
Notebook note = new Notebook();
note.storeNote("happy");
note.storeNote("hello there");
note.storeNote("work at 5");
note.storeNote("BBQ Time");
note.storeNote("UNI!!!!");
note.storeNote("Dont miss lecture at 9:15");
System.out.println(note.numberOfNotes());
note.showNote(1);
note.searchNotes("hap");
}}
public class Notebook{
/**
* Perform any initialization that is required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList();
}
/**
* Store a new note into the notebook.
* @param note The note to be stored. …
Run Code Online (Sandbox Code Playgroud) 我是否必须遍历每个元素才能转换ArrayList<String[]>
为String[][]
或者是否有更有效的方法?
我有一个类Method的对象.我调用m.getParameterTypes()
了创建方法参数的数组参数.
我还有一个对象阵列arr.我想检查arr中的对象是否与params中的类型相同(并且顺序相同).
我所做的是创建一个数组类,其中包括arr中每个对象的类.然后我尝试将类与params进行比较.事实证明它们永远不会相等,因为classes数组中的类格式为java.lang.String
,而param数组中的参数格式为String
.我如何比较阵列?如何更改其中一个的格式?
我正在使用a Comparator
来对viewcoped托管bean中List<Entity>
的它的属性(类型Date
)进行排序.
问题是我一直得到ClassCastException
如下 -
java.lang.ClassCastException:pkg.db.BaseArStatus无法强制转换为pkg.db.BaseArStatus.
为什么不能投BaseArStatus
来BaseArStatus
?是因为BaseArStatus
是Entity
吗?
这个问题对我来说真的很奇怪,因为我每次都没有得到例外.在构建和部署应用程序时,大多数工作正常(运行没有任何问题)但有时(即使我做同样的事情 - 构建和部署)它在运行时失败了ClassCastException
.
为什么这种情况有时只发生而不是一直发生?是因为我在托管bean中使用它吗?
这就是托管bean的样子 -
@ManagedBean
@ViewScoped
public class MyBean {
@PersistenceContext(unitName = "myPU")
private EntityManager em;
public void myMethod() {
List<BaseArStatus> basList = this.fetchAllBaseArStatus();
Collections.sort(basList, new Comparator<BaseArStatus>() {
@Override
public int compare(BaseArStatus o1, BaseArStatus o2) {
return o1.getMonthDate().compareTo(o2.getMonthDate());
}
});
//...
Run Code Online (Sandbox Code Playgroud)
而实体BaseArStatus
-
@Entity
@Table(name = "base_ar_status")
@NamedQueries({ …
Run Code Online (Sandbox Code Playgroud) 我编写了以下使用的JUnit测试ArgumentMatchers
。
MyClass classUnderTest = new MyClass();
class AnyBooleanMatcher extends ArgumentMatcher<Boolean> {
public boolean matches(Object argument) {
return ((Boolean) argument).equals(Boolean.TRUE);
}
}
class MyObjectMatcher extends ArgumentMatcher<MyObject> {
public boolean matches(Object argument) {
return ((MyObject) argument).getValue().equals("123");
}
}
final Service mockService = mock(Service.class);
when(mockService.search(Matchers.argThat(new MyObjectMatcher()),
Matchers.argThat(new AnyBooleanMatcher())));
classUnderTest.callMethod(mock(ActionEvent.class));
verify(mockService).search(Matchers.argThat(new MyObjectMatcher()),
Matchers.argThat(new AnyBooleanMatcher()));
Run Code Online (Sandbox Code Playgroud)
不幸的是,我NullPointerException
在when
声明中总是收到。
我什至知道为什么:argThat
返回null
JavaDoc中指定的。但是,我不知道为什么示例中的建模方式完全相同时,我的测试为什么不起作用。
我明白这个问题很奇怪,不是我惯用的风格.我目前正在使用peersim中的和弦实现做一个项目.下面的代码显示了一个大整数,并在其上执行了一些操作.我chordId
是一个对象的哈希,为什么要使用add()
?这个用途的目的是什么?
BigInteger base;
if (j == 0)
base = BigInteger.ONE;
else {
base = BigInteger.valueOf(2);
for (int exp = 1; exp < j; exp++) {
base = base.multiply(BigInteger.valueOf(2));
}
}
BigInteger pot = cp.chordId.add(base);
Run Code Online (Sandbox Code Playgroud)
在他之前,和弦Id只是idlength的随机整数,它是128位.
因此,我的问题是上面add()
用于??? 的代码段是什么?
[编辑]
为了使这个问题更加清晰,我们将其置于一个透视图中:
cp.fingerTable[j] = findId(pot, 0, Network.size() - 1);
Run Code Online (Sandbox Code Playgroud)
被调用,它试图找到Pot的Id但是它总是返回错误,因为在这个方法中创建的chordId不存在.我不确定如何更换锅或是否完全取出锅.
[EDIT2]
findId
看起来像这样(这不是我的代码,因此我的困惑:))
public Node findId(BigInteger id, int nodeOne, int nodeTwo) {
if (nodeOne >= (nodeTwo - 1))
return Network.get(nodeOne);
int middle = (nodeOne + nodeTwo) / …
Run Code Online (Sandbox Code Playgroud) 为什么这个for循环计数不正确?如果我将变量设置runs
为3,则循环运行4次.(一个额外的案例.)
提前致谢!
for (int i = runs; i >= 0; i--)
{
System.out.println("Input Duration of Trip");
Scanner timeCalc = new Scanner(System.in);
System.out.print("Hours ==> ");
int hour = timeCalc.nextInt();
System.out.print("Minutes ==> ");
int minute = timeCalc.nextInt();
System.out.println("You entered: " + hour + " hour(s) and " + minute + " minutes");
System.out.println();
time = convertHoursMinutesToDouble(hour, minute);
totalTime += time;
}
Run Code Online (Sandbox Code Playgroud)