我想找到给定输入字符串中某个子字符串最后一次出现的位置(或索引)str.
例如,假设输入的字符串是str = 'hello'和子字符串是target = 'l',那么它应该输出3.
我怎样才能做到这一点?
目前我正在进行代码优化,我正在使用try .. finally块来尊重我的对象.
但我感到困惑的是,当我在finally块中创建对象的空引用时,如何管理返回对象.??
在try块中返回一个对象时,它会在编译期间创建一个预编译的语句吗?或者在返回语句时在堆中创建新引用?或者只返回对象的当前引用?
以下是我的研究代码.
public class testingFinally{
public static String getMessage(){
String str = "";
try{
str = "Hello world";
System.out.println("Inside Try Block");
System.out.println("Hash code of str : "+str.hashCode());
return str;
}
finally {
System.out.println("in finally block before");
str = null;
System.out.println("in finally block after");
}
}
public static void main(String a[]){
String message = getMessage();
System.out.println("Message : "+message);
System.out.println("Hash code of message : "+message.hashCode());
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
在最后块中
的str:-832992604
的最后块中的try块阻塞哈希代码 消息:Hello world 哈希消息代码:-832992604
当我看到返回对象和调用对象具有相同的哈希码时,我感到非常惊讶.所以我对对象引用感到困惑.
请帮我清楚这个根本.
目前,应用程序正在处理数据库中的多个模式,因此我们有一个存储主表的公共模式,而我们存储客户特定数据的其他模式.
所以,具体情况就像
(下表仅供参考)
主表AnimalType这是居住common模式,而Animal表是适用于所有客户端的模式,如schema1,schema2... schemaN.
我们正在使用Grails哪个默认使用Hibernate,关系就像
class AnimalType {
String type
static mapping = {
datasources(['index'])
}
}
class Animal {
String name
AniamlType animalType
}
Run Code Online (Sandbox Code Playgroud)
因此,当我启动应用程序时,它显示以下异常:
引起:org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败; 嵌套异常是org.hibernate.MappingException:表动物的关联引用了一个未映射的类:org.sample.AnimalType
我从中理解的是因为,Animal试图引用AnimalType它自己的模式,但AnimalType在那里不存在.
所以,基本上我想映射Animal到AnimalType指向common架构.
Grails中的语法如下所示
class Animal {
String name
@(POINTING TO COMMON SCHEMA)
AnimalType animalType
}
Run Code Online (Sandbox Code Playgroud) 我有一个表示具有唯一Id属性的DB-Entries的类.可以仅基于此属性实现equals()和hashcode()方法
@Override public int hashCode()
{ return id;
}
@Override public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Task other = (Task) obj;
if (id != other.id)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud) 使用iText合并不同宽度的文档时遇到问题.
下面是我用来合并的代码.
public static void doMerge(List<InputStream> list, OutputStream outputStream) throws Exception {
Rectangle pagesize = new Rectangle(1700f, 20f);
com.itextpdf.text.Document document = new com.itextpdf.text.Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
document.setPageSize(pagesize);
com.itextpdf.text.pdf.PdfContentByte cb = writer.getDirectContent();
for (InputStream in : list){
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++){
document.newPage();
//import the page from source pdf
com.itextpdf.text.pdf.PdfImportedPage page = writer.getImportedPage(reader, i);
//calculate the y for merging it from top
float y = document.getPageSize().getHeight() - page.getHeight();
//add …Run Code Online (Sandbox Code Playgroud) 我写一些这样的代码:
public static void function7() {
String str = "123";
String str2 = "123";
synchronized (str) {
if(str != null) {
str2 = "123";
}else{
str = "456";
}
System.out.println(str2);
}
}
Run Code Online (Sandbox Code Playgroud)
代码编译良好。但是Eclipse的一个插件,查找错误,给出以下错误报告:
常量字符串在JVM加载的所有其他类中进行实习和共享。因此,这可能会锁定其他代码可能也会锁定的内容。这可能会导致非常奇怪且难以诊断的阻塞和死锁行为。
到底是什么意思?
我画了一条线然后是一个点,然后我想检查点是否在线上.我在数组中采用了一个线坐标(因为有多个线).我想检查最后一行的当前点吗?
if (positionX1 == positionX2 && positionY1 == positionY2) {
float m = line.getSlope(
drawLines[currentLines - 1][2], drawLines[currentLines - 1][3],
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
m = Float.parseFloat(df.format(m));
float c = line.getIntercept(
drawLines[currentLines - 1][2], drawLines[currentLines - 1][3],
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
c = Math.round(c);
m1 = line.getSlope(positionX2, positionY2,
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
m1 = Float.parseFloat(df.format(m1));
System.out.println(m + " " + m1);
c1 = line.getIntercept(positionX2, positionY2,
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
c1 = Math.round(c1); …Run Code Online (Sandbox Code Playgroud) 由于我是java新手,我想知道,1.当我创建一个空类时,定义了多少个默认构造函数?2.我是否需要定义复制构造函数,或者它是否在C++中自动定义?
我的项目在"\ BankPrj"文件夹中有
(1).com文件夹
(2) ,,Account.java 文件Customer.javaTestBanking.java
(3)主要课程在 TestBanking.java
这是错误..

这是我的完整项目
https://docs.google.com/folder/d/0BzMe2Ya7vhrAem5rcEljc3k1cFE/edit
请纠正错误.
我从测试文件夹本身执行了TestBanking.class.
在我开发基于桌面的应用程序时,有一个.pdf格式的用户手册,应该在用户点击帮助菜单项时打开.
我不知道从swing应用程序打开.pdf文件的方法,所以如何在我jframe或任何pdf文件查看器(如acrobat reader)中打开它.
此代码存在许多问题
public class LineEx extends JFrame implements MouseMotionListener,MouseListener{
int x1,y1,x2,y2;
public LineEx(){
JLabel image=new JLabel("");
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int r=chooser.showOpenDialog(new JFrame());
File file=chooser.getSelectedFile();
if(r==JFileChooser.APPROVE_OPTION){
try {
BufferedImage bf;
bf = ImageIO.read(file);
ImageIcon icon=new ImageIcon(bf);
image.setIcon(icon);
image.setHorizontalAlignment(JLabel.CENTER);
} catch (IOException ex) {
Logger.getLogger(LineEx.class.getName()).log(Level.SEVERE, null, ex);
}
}
JScrollPane jsp=new JScrollPane(image);
getContentPane().add(jsp);
image.addMouseListener(this);
image.addMouseMotionListener(this);
this.pack();
}
public static void main(String ar[]){
LineEx line=new LineEx();
line.setVisible(true);
line.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY(); …Run Code Online (Sandbox Code Playgroud) 为什么这个条件返回true,因为我们知道==如果两个变量都具有相同的引用,则返回true,但这里的引用不相同但仍然在循环中输入并打印Hello World.
String var1="hi";
String var2="hi";
if(var1==var2){
System.out.println("Hello World");
}
Run Code Online (Sandbox Code Playgroud)