我试着编写从数组中删除重复元素的泛型函数.
public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {
//do quicksort
Arrays.sort(arr);
ArrayList<E> list = new ArrayList<E>();
int i;
for(i=0; i<arr.length-1; i++) {
if(arr[i].compareTo(arr[i+1]) != 0) { //if not duplicate, add to the list
list.add(arr[i]);
}
}
list.add(arr[i]); //add last element
return list;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,您无法传递像int []数组这样的原始类型,因为我通过在Comparable接口中定义的compareTo()方法来比较元素.
我注意到第一行(方法声明):
public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {
Run Code Online (Sandbox Code Playgroud)
它怎么说"扩展可比"?
可比较是一个接口,为什么它不"实现可比"?这是我第一次编写泛型函数,所以我对这些细节感到困惑.(任何疑惑都会阻止我理解..)
编辑:发现此文章与此主题相关.
我正在创建一个iPad添加,我正在使用MPMoviePlayerViewController播放视频.
该视频占据了整个iPad屏幕.
playerViewController.view.frame = self.view.frame;
Run Code Online (Sandbox Code Playgroud)
我需要一种方法让用户能够按下按钮转到不同的屏幕.
我注意到,当我创建MPMoviePlayerViewController时,导航控制器中会自动显示一个完成按钮.
我的问题:
a.)无论如何都要挂钩现有的完成按钮?基本上我只是想解雇视图控制器.
b.)如果这不起作用.如何添加自己的自定义按钮?如上所述,MPMoviePlayerViewController占据整个屏幕.我的一个想法是在框架中创建MPMoviePlayerViewController并留下一些垂直空间,以便我可以添加自己的工具栏.
我更喜欢有关如何实施的建议.)?
如果这是不可能的,可能有一些关于如何通过按下按钮来关闭MPMoviePlayerViewController的建议?
所有帮助赞赏.
这是我的第一个问题.
编写一些代码,我从g ++收到此错误:"在此范围内未声明实体",在此上下文中:
#ifndef Psyco2D_GameManager_
#define Psyco2D_GameManager_
#include <vector>
#include "Entity.h"
namespace Psyco2D{
class GameManager{J
private:
std::vector<Entity> entities;
};
}
#endif
Run Code Online (Sandbox Code Playgroud)
这是Entity.h的内容:
#ifndef Psyco2D_Entity_
#define Psyco2D_Entity_
#include <string>
#include "GameManager.h"
#include "EntityComponent.h"
namespace Psyco2D{
class Entity{
friend class GameManager;
private:
/* Identificatore */
std::string _name;
/* Components list */
std::map<const std::string, EntityComponent*> components;
protected:
Entity(const std::string name);
public:
inline const std::string getName() const{
return this->_name;
}
void addComponent(EntityComponent* component, const std::string name);
EntityComponent* lookupComponent(const std::string name) const;
void deleteComponent(const std::string …Run Code Online (Sandbox Code Playgroud) 这似乎是一件相当普遍的事情,而且我已经设法自学了我需要的一切,除了我现在有一个问题,这是我的故障排除.
int nonBlockingPOpen(char *const argv[]){
int inpipe;
pid_t pid;
/* open both ends of pipe nonblockingly */
pid = fork();
switch(pid){
case 0: /*child*/
sleep(1); /*child should open after parent has open for reading*/
/*redirect stdout to opened pipe*/
int outpipe = open("./fifo", O_WRONLY);
/*SHOULD BLOCK UNTIL MAIN PROCESS OPENS FOR WRITING*/
dup2(outpipe, 1);
fcntl(1, F_SETFL, fcntl(1, F_GETFL) | O_NONBLOCK);
printf("HELLO WORLD I AM A CHILD PROCESS\n");
/*This seems to be written to the pipe immediately, blocking or not.*/
execvp(*argv, …Run Code Online (Sandbox Code Playgroud) 我的Eclipse 3.6/PyDev设置只是将pydev升级到1.6.0.2010071813并且调试不再有效.我的默认python解释器是3.1虽然我怀疑这很重要.在pydev的Eclipse升级之前,它运行得非常好.
可能每个人在开发过程中至少遇到过一次这个问题:
while(/*some condition here that somehow never will be false*/)
{
...
yourvector.push_back(new SomeType());
...
}
Run Code Online (Sandbox Code Playgroud)
当您看到程序开始耗尽所有系统内存时,您的程序会挂起,并且您的系统开始像疯了一样交换.如果你不能足够快地识别问题并终止进程,你可能会在几秒钟内得到一个无响应的系统,你的鼠标指针甚至都没有移动.您可以使用"内存不足"错误(可能需要几分钟的时间)等待程序崩溃,或者在计算机上点击重置.
如果您无法立即追踪该错误,那么您将需要多次测试和重置以找出哪个非常烦人...
我正在寻找一种可能的跨平台方式来防止这种情况.最好的是调试模式代码,如果它分配了太多内存,它会退出程序,但是如何跟踪分配的内存量呢?覆盖全局new和delete操作符无济于事,因为我在delete中调用的free函数不会知道释放了多少字节.
任何想法都赞赏.
我确信有一个更惯用的ruby方式来编写下面的代码:
@var = obj['blah'] unless obj['blah'].nil?
Run Code Online (Sandbox Code Playgroud)
我已经完成了这些工作(见下文),并且必须有一个更好的方法!
@num_x = obj['num_x'] unless obj['num_x'].nil?
@num_y = obj['num_y'] unless obj['num_y'].nil?
@num_iterations = obj['num_iterations'] unless obj['num_iterations'].nil?
@pe = obj['pe'] unless obj['pe'].nil?
Run Code Online (Sandbox Code Playgroud)
我觉得||=操作员可能很有用,但似乎无法解决如何使用它.
我有一个谷歌应用引擎应用程序,我想扩展我的一个实体定义.如何确保现有实体对象正确初始化新字段?现在的对象,下次我查询它们时,是否只有默认值?我想添加一个StringListProperty.
如你所知苹果使用javascript/sproutcore作为mobileme.com和iwork.com的产品.他们用什么作为后端?javascript服务器端?