我如何通过反射访问受保护的类?
这个不起作用:
Class.forName("java.awt.AWTInvocationLock");
Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被多次询问和回答.但是几乎所有解决方案都具有O(n ^ 2)的计算复杂度.
我正在寻找O(n log n)复杂度的解决方案.有人可以建议吗?
谢谢堆,Chaitanya
这是我的样本对象
public class Dummy {
private long id;
private String name;
/**
* @return the id.
*/
public long getId() {
return id;
}
/**
* @param id the id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name.
*/
public String getName() {
return name;
}
/**
* @param name the name to set.
*/
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我有这些对象的列表,我需要获取列表中该对象中存在的ID列表.我可以轻松地做到这一点
List<Long> ids = Lists.newArrayList();
for(Dummy …Run Code Online (Sandbox Code Playgroud) 我需要做自己的arraylist课程.我正在尝试编写插入方法,如果在此索引中存在已存在的对象,我将面临将对象添加到索引0的问题.它适用于其他指数,但不适用于0.任何帮助将不胜感激?
public void insert(int index, Object object) throws IndexOutOfBoundsException {
if(index < 0) {
System.out.println("Niepoprawny index!");
throw new IndexOutOfBoundsException();
}
if(index > array.length - 1 || array[index] != null) {
// zwi?kszenie rozmiaru tablicy
// je?li jest zbyta ma?a lub na danym miejscu istnieje jaki? obiekt
Object[] temp = new Object[array.length + index];
System.arraycopy(array, 0, temp, 0, array.length);
array = temp;
}
// przesuni?cie o 1 w prawo wszystkich elementów
System.arraycopy(array, index - 1, array, index, array.length - index);
array[index] …Run Code Online (Sandbox Code Playgroud)