假设我们尝试实现合并排序算法,给定一个数组数组来合并什么是更好的方法,这个:
public void merge(ArrayList<ArrayList<E>> a) {
ArrayList<ArrayList<E>> tmp = new ArrayList<ArrayList<E>>() ;
while (a.size()>1) {
for (int i=1; i<a.size();i+=2) {
tmp.add(merge(a.get(i-1),a.get(i)));
}
if (a.size()%2==1) tmp.add(a.get(a.size()-1));
a = tmp;
tmp = new ArrayList<ArrayList<E>>() ;
}
}
Run Code Online (Sandbox Code Playgroud)
或这个 :
public void merge(ArrayList<ArrayList<E>> a) {
ArrayList<ArrayList<E>> tmp = new ArrayList<ArrayList<E>>(),tmp2 ;
while (a.size()>1) {
for (int i=1; i<a.size();i+=2) {
tmp.add(merge(a.get(i-1),a.get(i)));
}
if (a.size()%2==1) tmp.add(a.get(a.size()-1));
tmp2 = a;
a = tmp;
tmp = tmp2;
tmp.clear();
}
}
Run Code Online (Sandbox Code Playgroud)
这样可以很清楚,我在做什么,是要合并每对夫妇的邻居的一个,并把结果合并阵列,阵列的外部阵列TMP,合并所有的夫妇后,一种方法是清除一个,然后移动 …
假设我们有:
class A {
protected:
int* iArr;
float *fArr;
public:
A(int* iArr,float *fArr);
~A();
}
class B : public A {
private:
double *dArr;
public:
B(int* iArr,float *fArr,double *dArr);
~B();
}
Run Code Online (Sandbox Code Playgroud)
我的意思是它只会调用B的析构函数,但是当我在Visual C++上运行它时,我发现在破坏B的实例时它会调用A然后调用B析构函数.
那么在派生类中编写析构函数的正确方法是什么?我总是需要假设父类会处理删除所有内容,但只有派生类有什么?
编辑:
如果是这样,那么如果孩子只用重写函数扩展父类,那是否意味着我将孩子的析构函数留空了?
如果我想改变它怎么办?我的意思是,如果我只想要调用子析构函数?有没有办法做到这一点?
这是我的Generic bubbleSorter的Java代码:
public class BubbleSorter<E extends Comparable<E>> {
E[] a;
void swap(int i, int j) {
E temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void bubbleSort(E[] a) {
this.a=a;
for (int i=0 ;i<a.length;i++) {
for (int j=0;j<a.length;j++) {
if ( a[i].compareTo(a[j]) > 0) swap(i,j);
}
}
}
}
public interface Comparable<E> {
public int compareTo(E e);
}
Run Code Online (Sandbox Code Playgroud)
这是一个使用它的例子:
public class Test {
public static void main (String arg[]) {
Rational[] a = new Rational[3];
a[0]=Rational.rationalFactory(9,2);
a[1]=Rational.rationalFactory(1,3);
a[2]=Rational.rationalFactory(10,11);
Complex[] b = new Complex[3]; …Run Code Online (Sandbox Code Playgroud) 我尝试使用TreeSet但它禁止双打有没有办法改变它?如果没有办法,我应该用什么来存储(相等和不相等)元素排序?
我是C++的新手,我熟悉Java.我想知道,当我开始寻找在C++代码的第一件事是,类本身(而不是成员)不具有访问说明这样的private,protected和public.这里和这里都有例子.
public class A { // This line.
private class B { } // Not this line.
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
这个主要给予ExecutorService 1000 runnables(测试者)所有他们所做的就是睡10毫秒,然后将1添加到静态计数器,主要假设等待直到所有执行完成,但是计数器达到了970左右执行......为什么?
public class Testit {
public static void main (String arg[]) {
int n=1000;
ExecutorService e1 = Executors.newFixedThreadPool(20);
for (int i=0 ;i <n ;i++) {
e1.execute(new Tester());
}
e1.shutdown();
try {
e1.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Executed "+Tester.tester()+" Tasks.");
}
}
Run Code Online (Sandbox Code Playgroud)
和测试者类:
public class Tester implements Runnable {
public static long tester=0;
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally { tester++; }
}
public static …Run Code Online (Sandbox Code Playgroud) 我正在尝试将文件从任何位置(包括内部设备存储)移动到 SD 卡,
为此我有
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
在我的清单中,并通过检查是否使用以下方法授予权限来检查它是否有效:
Contex.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") 和 Contex.checkCallingOrSelfPermission("android.permission.READ_EXTERNAL_STORAGE")
我尝试了 3 种不同的方法来做到这一点:
这种方法:
public static void move(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
Run Code Online (Sandbox Code Playgroud)original.renameTo(newPath1);
我阅读了很多关于此的 SO 帖子,我尝试过的所有解决方案都不起作用,我确保安装了 SD 卡,并且在尝试时我没有将我的设备连接到 PC。我什至尝试了 2 种不同的带有 SD 卡的设备。请注意,我可以使用 BitmapFactory.decode() 以完全相同的路径访问图像,并且可以将该图像加载到 imageview。
我总是收到 EACCES(权限被拒绝)。我不知道还能做什么来解决这个问题:(
11-13 11:11:54.358 19192-19372/xaday.ofek.ron.xaday W/System.err: java.io.FileNotFoundException: /storage/sdcard1/DCIM/thai2/IMG-20150702-WA0001.jpg: open …Run Code Online (Sandbox Code Playgroud) 考虑c中的LinkedList例子,其中我显式预先分配堆栈上的N个节点结构用作池或节点堆栈,而不是使用慢mallocs和frees,(我不需要在运行中释放节点的功能,所以堆栈会这样做):
#define N 40000
typedef struct node_t {
void * ele;
struct node_t * next;
}node,*Pnode;
node Stack[N];//memory allocation for the linkedlist nodes
int sp=0;
Pnode createNode(void * x) {
Pnode temp=&Stack[sp++];
temp->ele=x;
temp->next=NULL;
return temp;
}
Run Code Online (Sandbox Code Playgroud)
当我试图在JAVA中模仿上面的想法时,那就是我想出来的......你能完成这个类,使Node []堆栈成为一个节点对象数组,该内存是在STACK中预先分配的吗?
public class Node<E> {
private final static int n = 40000;
private static Node[] stack = ?
private static int sp = 0;
private E ele;
private Node next;
private Node () {}
public Node createNode(E e) {
stack[sp].ele=e;
stack[sp].next=null;
return …Run Code Online (Sandbox Code Playgroud) 我知道在java中实现单例模式的两种方法,我想知道哪一个更好,为什么.
第一种方式是:
第二种方式是:
我倾向于认为即使第二种方法是最常见的,第一种方法可能会产生更好的代码可读性,这两种方法在运行时复杂性上看起来同样有效,所以我真的没有理由为什么第二种方法更常见,认为更好的做法......
开导我!
试过:
NestedScrollView ns =(NestedScrollView) findViewById(R.id.nested_scroll);
ns.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
}
});
Run Code Online (Sandbox Code Playgroud)
但是卡住了,有没有人有想法?
只是为了清除我想要的东西 - 我希望能够观察滚动状态(如在RecyclerView的addOnScrollListener中)并且只在滚动结束(空闲)时检查一次,如果用户滚动到NestedScrollView的末尾.
android scrollview android-scrollview nestedscrollview android-nestedscrollview
java ×5
android ×2
c++ ×2
c# ×1
class ×1
collections ×1
destructor ×1
generics ×1
inheritance ×1
memory ×1
new-operator ×1
performance ×1
privileges ×1
scrollview ×1
singleton ×1
sorting ×1
static ×1