我试图通过比较结果对象与原始对象来验证序列化和反序列化例程.例程可以序列化任意和深度嵌套的类,因此我想要一个比较例程,可以给出原始和最终实例,并反过来遍历每个值类型并比较值,并迭代地潜入引用类型以比较值.
我已经尝试了Apache Commons Lang,EqualsBuilder.reflectionEquals(inst1, inst2)
但这似乎没有进行非常深入的比较,它只是比较参考类型的平等而不是深入研究它们:
以下代码说明了我的问题.第一次调用reflectionEquals
返回true但第二次返回false.
是否有人可以推荐的图书馆例程?
class dummy {
dummy2 nestedClass;
}
class dummy2 {
int intVal;
}
@Test
public void testRefEqu() {
dummy inst1 = new dummy();
inst1.nestedClass = new dummy2();
inst1.nestedClass.intVal = 2;
dummy inst2 = new dummy();
inst2.nestedClass = new dummy2();
inst2.nestedClass.intVal = 2;
boolean isEqual = EqualsBuilder.reflectionEquals(inst1.nestedClass, inst2.nestedClass);
isEqual = EqualsBuilder.reflectionEquals(inst1, inst2);
}
Run Code Online (Sandbox Code Playgroud) 我正在研究一个读取/var/log/auth.log文件的监控程序.我正在使用Apache Commons IO Tailer
类来实时读取文件.首先,我想在一个简单的文件上测试实时阅读部分,并在控制台行中手动输入一些代码.这是我的代码:
public class Main {
public static void main(String[] args) {
TailerListener listener = new MyListener();
Tailer tailer = Tailer.create(new File("log.txt"), listener, 500);
while(true) {
}
}
}
public class MyListener extends TailerListenerAdapter {
@Override
public void handle(String line) {
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud)
从终端:sudo echo "Hello" >> log.txt
问题是当我尝试在文件中手动编写某些东西时,它不会在控制台中打印它.我试图找到一个使用Tailer类的具体例子,但没有运气.我在这做错了什么?
Android的addHeaderView()可用于在单个ListView中添加多个标头吗?有人可以举例说明如何做到这一点吗?
通过操作IconicAdapter类,我能够完成我想要的...有什么理由不这样做吗?我觉得这可以修改为更高级的实现.在我的情况下,我知道我将有两个部分,每个部分有一个标题+ 2行.
class IconicAdapter extends ArrayAdapter<String> {
IconicAdapter() {
super(ContactTabProfileResource.this, R.layout.row_iconic, mArrayList);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = null;
if(position == 1 || position == 5) { // phone
row = inflater.inflate(R.layout.row_iconic, parent, false);
TextView label =(TextView)row.findViewById(R.id.label);
label.setText(mArrayList.get(position));
ImageView icon = (ImageView)row.findViewById(R.id.rowicon);
icon.setImageResource(R.drawable.icon_phone);
} else if (position == 2 || position == 6) { // email
row = inflater.inflate(R.layout.row_iconic, parent, false);
TextView label =(TextView)row.findViewById(R.id.label);
label.setText(mArrayList.get(position));
ImageView icon = …
Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来存储类的新实例作为Java哈希映射中的值.这个想法是由Java教师给我的,以便创建一个可用于我正在处理的程序的数据存储结构.
他向我推荐的想法是使用存储计算机名称的hashmap作为键,值将是InfoStor.class类的新实例.InfoStor包含getName(),setName(),getMemory()等方法......
我有类和方法几乎设置基本测试,看看它是否可行.我遇到的问题是,一旦我在hashmap中创建了一个新条目,我就无法弄清楚如何使用InfoStor中的方法.
这是我到目前为止的代码;
VMware.class
import java.util.HashMap;
public class VMware {
public static void main(String[] args) {
HashMap <String, Object> mapper = new HashMap();
mapper.put("NS01", new InfoStor("NS01"));
//mapper.get("NS01").
}
}
Run Code Online (Sandbox Code Playgroud)
InfoStor.class
public class InfoStor {
private String vmName;
private String platform;
private Integer memory;
public InfoStor (String name) {
vmName = name;
}
String getName(){
return vmName;
}
void setPlatform(String p){
platform = p;
}
String getPlatform(){
return platform;
}
void setMemory(Integer m){
memory = m;
}
Integer getMemory(){
return …
Run Code Online (Sandbox Code Playgroud) 我正在使用Eclipse 4.2和Android SDK.
我想知道是否可以com.example
在键入应用程序名称时更改"新建Android应用程序"向导中显示的默认包ID ?
我希望它默认为我自己的包ID,这样我每次都不需要更正包名称字段.
这可能吗?请解释一下.
我刚刚下载了OEPE(Kepler)并安装了m2e和m2e-wtp连接器.我发现在这条路径下:首选项 - > Maven->生命周期映射 - >打开工作区生命周期映射数据有一个预先配置的xml文件,该文件说maven应该忽略AspectJ的编译目标,我认为这就是为什么AspectJ运行时库是未添加到项目中,因此eclipse不会将项目识别为AspectJ项目.
<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<versionRange>1.6</versionRange>
<goals>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
Run Code Online (Sandbox Code Playgroud)
我在xml文件中注释掉了这些行并再次重新加载.现在IDE不会忽略生命周期中的AspectJ插件标记,但是pom文件抱怨它无法识别执行标记.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
使用indigo,m2e-wtp能够识别<execution>
aspectj插件的标签,并能够自动将AspectJ运行时库添加到项目中,尽管在开普勒中并非如此.(我认为m2e-wtp的工作是将一个AspectJ项目从pom中取出但不太确定.)
顺便说一句.我怎么能像Indigo那样让事情发挥作用?我知道我可以右键单击项目并将其转换为Aspect项目以解决问题,但我希望IDE和插件从pom文件中实现该项目需要AspectJ jar.任何的想法?
考虑一下代码:
public abstract class Item<T> implements Comparable<T>
{
protected T item;
public int compareTo(T o)
{
return 0; // this doesn't matter for the time being
}
}
public class MyItem<T> extends Item<String>
{
T object;
}
public class Foo<T>
{
protected ArrayList<T> list;
}
public class Bar<V> extends Foo<MyItem<V>>
{
public void sort()
{
Collections.sort(list);
}
}
Run Code Online (Sandbox Code Playgroud)
排序调用给出错误:
绑定不匹配:类型集合的泛型方法sort(List <T>)不适用于参数(ArrayList <MyItem <T >>).推断类型MyItem <T>不是有界参数的有效替代<T extends Comparable <?超级T >>
为什么这是错的?
如果MyItem<V>
实施Comparable
那么为什么它不是替代品呢?
对不起,如果有人询问,但我觉得这个问题有点具体.
我正在尝试使用嵌入代码工具向Sharepoint 2013页面添加一些样式.它工作正常,直到我保存页面,然后它删除<style>
和</style>
标签之间的所有文本?
所以我点击页面上的嵌入代码并输入以下内容
<style>
.sampleStyle {color:#fff}
</style>
Run Code Online (Sandbox Code Playgroud)
当它保存并重新打开时,我发现sharepoint已将代码更改为此
<style unselectable="on">
</style>
Run Code Online (Sandbox Code Playgroud) 我在Java中遇到异常处理问题,这是我的代码.当我尝试运行此行时出现编译器错误:throw new MojException("Bledne dane");
.错误是:
异常MojException永远不会在相应的try语句的主体中抛出
这是代码:
public class Test {
public static void main(String[] args) throws MojException {
// TODO Auto-generated method stub
for(int i=1;i<args.length;i++){
try{
Integer.parseInt(args[i-1]);
}
catch(MojException e){
throw new MojException("Bledne dane");
}
try{
WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args[0]));
System.out.println(args[i]+" : "+a.wspolczynnik(Integer.parseInt(args[i])));
}
catch(MojException e){
throw new MojException(args[i]+" "+e.getMessage());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是MojException的代码:
public class MojException extends Exception{
MojException(String s){
super(s);
}
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个?
我在Ganesh和Sharma的书中提出了一个问题:oracle_certified_professional_java_se_7_programmer_exams_1z0-804_and_1z0-805.
一个问题是:
考虑以下程序并预测输出:
Run Code Online (Sandbox Code Playgroud)class Test { public static void main(String args[]) { String test = "I am preparing for OCPJP"; String[] tokens = test.split("\\S"); System.out.println(tokens.length); } }
a)0
b)5
c)12
d)16
现在我明白\ S是正则表达式意味着将非空格字符视为分隔符.但我很困惑的是正则表达式如何进行匹配以及split产生的实际标记是什么.
我添加了代码来打印令牌,如下所示
for (String str: tokens){
System.out.println("<" + str + ">");
}
Run Code Online (Sandbox Code Playgroud)
我得到了以下输出
16
<>
< >
<>
< >
<>
<>
<>
<>
<>
<>
<>
<>
< >
<>
<>
< >
Run Code Online (Sandbox Code Playgroud)
所以很多空字符串令牌.我只是不明白这一点.
我会想到,如果分隔符是非空格字符,那么在上面的文本中,所有字母字符都可以作为分隔符,所以如果我们匹配导致空字符串的标记,也许应该有21个标记.我只是不明白Java的正则表达式引擎如何解决这个问题.是否有任何正则表达的大师可以为我阐明这些代码?