首页 \ 问答 \ Java通用投射(Java generic casting)

Java通用投射(Java generic casting)

我有一个泛型类A<T> ,一个子类B扩展了A<String> 。 A类有一个返回它的方法(所以A<T> )。 问题是,如果我在B对象上调用该方法,它将返回A<String>并且我必须转换为B,但是B与A<String>相同,不是吗? 所以我不应该投...


I have a generic class A<T> and a subclass B extends A<String>. The class A have a method which returns this (so an A<T>). The problem is, if I call the method on a B object, it will return a A<String> and I have to cast to B, but B is the same as A<String>, isn't it ? So I shouldn't have to cast ...


原文:https://stackoverflow.com/questions/38973046
更新时间:2023-10-31 14:10

最满意答案

如果您想要获取数组中的所有名称字段,下面的代码应该可以使用

NSMutableArray* result=[[NSMutableArray alloc] init];

    NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"];
        sqlite3_stmt *statement;
        const char *query_statement = [ask UTF8String];
        const char *dbpath = [_databasePath UTF8String];
        if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
            {
                if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
                {
                    while (sqlite3_step(statement) == SQLITE_ROW)
                    {
                        NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                        //add your namefield here.
                       [result addObject:nameField];
                    }

                    sqlite3_finalize(statement);
                    sqlite3_close(_DB);
                }
            }

最后你可以做到

totalStrings=[[NSMutableArray alloc] initWithArray:result];

The code below should work in case you want to get all namefields in array

NSMutableArray* result=[[NSMutableArray alloc] init];

    NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"];
        sqlite3_stmt *statement;
        const char *query_statement = [ask UTF8String];
        const char *dbpath = [_databasePath UTF8String];
        if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
            {
                if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
                {
                    while (sqlite3_step(statement) == SQLITE_ROW)
                    {
                        NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                        //add your namefield here.
                       [result addObject:nameField];
                    }

                    sqlite3_finalize(statement);
                    sqlite3_close(_DB);
                }
            }

In the end you can do

totalStrings=[[NSMutableArray alloc] initWithArray:result];

相关问答

更多
  • 在NSOrderedSet类中有一个方法来获取数组[orderedSet array] 。 然后你可以[array mutableCopy]来获得一个可变数组。 或者使用[NSMutableArray arrayWithArray:[set array]]; 如果你更喜欢。 但请注意这一点: 这将为接收的有序集合返回一个代理对象,这就像一个不可变数组一样。 虽然您不能通过此代理对有序集合进行变异,但原始有序集合的变化将反映在代理中,并且由于没有进行有序集合的复制,它会自发地变化。 基本上你必须复制数组,如果 ...
  • 有许多方法可以复制数组:您可以使用 - [NSArray copy]获取不可变副本,或者[NSArray mutableCopy]使用可变副本。 不要忘记该副本添加了一个引用,因此您需要在某处发布或自动释放(如果您不使用GC)。 或者,您可以使用 - [NSMutableArray addObjectsFromArray:]。 举个例子,看起来你想在最后做这样的事情: [flashCardQuestionViewController setListNoOfOptionsInQuestion:optionTe ...
  • 使用options:NSJSONReadingMutableContainers NSJSONSerialization调用上的NSJSONReadingMutableContainers。 然后它创建的所有字典和数组都是可变的。 Use options:NSJSONReadingMutableContainers on your NSJSONSerialization call. Then all the dictionaries and arrays it creates will be mutable ...
  • 对于非常少量的数据,如几个字符串,但不是图像,您也可以使用更简单的NSUserDefaults。 这通常用于保存首选项或某些持久数据。 要保存它: [[NSUserDefaults standardUserDefaults] aString forKey:@"aStringKey]; if (![[NSUserDefaults standardUserDefaults] synchronize]) NSLog(@"not successful in writing the default prefs ...
  • 如果您想要获取数组中的所有名称字段,下面的代码应该可以使用 NSMutableArray* result=[[NSMutableArray alloc] init]; NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; sqlite3_stmt *statement; const char *query_statement = [ask UTF8String]; ...
  • 要将CGPoint存储在NSMutableArray ,请执行此操作 NSMutableArray *yourCGPointsArray = [[NSMutableArray alloc] init]; [yourCGPointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(100, 100)]]; //Now getting the cgpoint back CGPoint point = [[yourCGPointsArray object ...
  • 您只需创建一个对象tmpItem 。 这将添加到数组中,并且在while循环的下一次运行中,您不是要创建新的tmpItem而是修改旧的tmpItem并将其添加到数组中。 因此,您将得到一个包含两个指向同一对象tmpItem (具有最新状态)的指针的Array。 解决方案:在while循环中创建tmpItem 。 You just create one object tmpItem. This will be added to the array and in the next run of the whil ...
  • NSMutableArray * muArr = [NSMutableArray new]; for(NSDictionary * dict in yourMuArr) { [muArr addObject:dict[@"resim"]]; } NSArray * allImages = [NSArray arrayWithArray:muArr]; NSMutableArray * muArr = [NSMutableArray new]; for(NSDictionary * dict in yo ...
  • 问题是你在布尔变量中保存SQLite调用的返回值。 您应该将这些变量更改为int ,与sqlite3_xxx()函数调用return的类型相同。 线索是您在修订后的问题中提供的错误消息: 常量101与'BOOL'(又名'bool')表达式的比较总是错误的。 当您为64位目标构建时, BOOL被定义为bool类型。 并且bool类型将采用任何非零值并将其更改为1 。 但是,对于非64位目标, BOOL被定义为signed char (它不会将非零值映射到1 ),这就是为什么你的代码在针对某些目标而不是其他目标 ...
  • 尝试调试你的代码...在NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];上设置一个断点NSString *field2Str = [[NSString alloc]initWithUTF8String:field2]; 行,并检查field2是否为空... 尝试像这样打开db: NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"speakerdb" ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)