我想知道何时实例化一个类的ArrayList.如果我通过给它一个初始大小来声明它像#2反对#1那么它会更快吗?或者,如果我确切地知道将添加多少列,我应该只给构造函数一个初始大小?
List<Column> columns = new ArrayList<Column>();List<Column> columns = new ArrayList<Column>(10);我在Tomcat中有一个连接到PostgreSQL数据库的webapp.我已将JDBC PostgreSQL驱动程序包含在lib/我的webapp文件夹中.我使用的IDE是Eclipse.
我收到这个错误
java.lang.ClassNotFoundException: org.postgresql.driver
Run Code Online (Sandbox Code Playgroud)
相关代码段:
Class.forName("org.postgresql.driver");
String connectionUrlString = "jdbc:postgresql://server_addr:5432/db_name";
Connection dbConnection = null;
dbConnection = DriverManager.getConnection (connectionUrlString, username, password);
Run Code Online (Sandbox Code Playgroud) 我试图调用一个方法来计算java中的平均值.但是当我编译它时总是输出'.class' expected到它的行:System.out.println("Average:"+ Average(double Value []));
这是我的代码:
public class q2
{
public static void main(String[] args) throws IOException
{
new q2().InputValue();
}
public void InputValue() throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double[] Value = new double[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Please enter a value: ");
Value[i] = Double.parseDouble(br.readLine());
}
System.out.println("Average: " + Average(double Value[]));
}
public double Average(double Value[])
{
double average = 0;
for (int n = 0; …Run Code Online (Sandbox Code Playgroud) 我收到像IX898或QX78这样的输入......我创建了正则表达式
String expression="(IX|MX|SX|QX)([0-9]+)";
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(address);
Run Code Online (Sandbox Code Playgroud)
如何找到它是否匹配以及它是否与如何获得数字匹配?
好的,所以我写了这个程序,它将计算一定的字母和空格,我想要它做的是让用户继续输入短语,它继续循环,直到用户进入退出终止.我很难看到在哪里放置while循环.我知道我应该在while循环下嵌套所有循环,当我这样做时,程序进入无限循环.
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
int countA=0,countE=0,countS=0,countT=0;
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
// Read in a string and find its length
System.out.print ("Enter a …Run Code Online (Sandbox Code Playgroud) 在我的程序代码中有很多线程.当我的程序开始和结束时,句柄的数量不会被释放.当程序第二次启动时,句柄数量增加.每次都会发生这种情况 此外,程序的线程数也增加了.谁能告诉我如何释放手柄?因为我的程序中存在句柄泄漏的问题.
运行时异常 - java.lang.ClassCastingException......
Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;
Run Code Online (Sandbox Code Playgroud)
我知道降级不安全,但为什么会这样呢?我也试图转换ArrayList到String以Integer对int,但我得到了同样的错误.
我很确定这很容易,但我找不到直截了当的答案.如何调用方法throws FileNotFoundException?
这是我的方法:
private static void fallingBlocks() throws FileNotFoundException
Run Code Online (Sandbox Code Playgroud) 我有一个多字符串网址,我必须从中选择最后几个字符,这是id的实际内容.但问题在于,id的长度不一致,即,如果一个id的长度为6,则其他的长度为5或4,依此类推.示例网址如下:
www.abc.com/xyz-123456
www.abc.com/pqr-5432
www.abc.com/lmn/opqr-25647
Run Code Online (Sandbox Code Playgroud)
如果特定id部分的长度相同,可能会更容易,我可以使用:
String abc = "www.abc.com/xyz-123456";
String id = abc.substring(abc.length()-6);
Run Code Online (Sandbox Code Playgroud)
但现在情况不同,因为所选网址中id部分的长度总是不一样,我怎样才能满足这种不同的id .. ???? 请任何帮助表示赞赏.
我有下面的代码片段
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");
String processedContentDate="2012-04-10 12:53:28.033";
java.util.Date parsedDate = dateFormat.parse(processedContentDate);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
parsedDate.getTime());
Run Code Online (Sandbox Code Playgroud)
我的解析日期为2012年4月10日星期二00:53:28,时间戳为2012-04-10 00:53:28.033. 我希望得到的时间与12:53:28.033完全一样(在我的原始字符串中)不是00:53:28.033. 没有得到为什么12:53:28转换为00:53:28.我该怎么办才能得到12:53:28?
编辑:得到答复后,我尝试了这个小程序,当前时间是14:34:38.899,但在两行,即第1行和第2行,我得到的解析日期2012-04-10 14:34:38.899按照回复我应该在第1行02:34:38.899作为日期格式yyyy-MM-dd hh:mm:ss.SSS")
java.util.Date date= new java.util.Date();
String strDate=date.toString();
java.util.Date parsedDate;
java.util.Date parsedDate2;
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");// line 1
SimpleDateFormat dateFormat2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");//line 2
try {
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
strDate=timestamp.toString();
parsedDate = dateFormat.parse(strDate);//line1
parsedDate2 = dateFormat2.parse(strDate);//line2
Run Code Online (Sandbox Code Playgroud)