0

I have the following question:

if I have the following line of code:

List<Position> allPos = posDBM.getAllPos();

Position is an object

posDBM is a SQLite Database Manager class, which manages the SQLite database, getAllPos() returns all database data.

The return type of getAllPos() is List<Position>.

If I want to initialize a List<> like this List<Position> pos = new, I have to specify the type of the List (ArrayList, LinkedList, etc.) .

So back to my question, what kind of List do I have, after I filled the list from the database?

I would guess it's an ArrayList , but I can't find any source to back this up. It's just a matter of interest...


Why can't access view.center.x / y

I'm just curious why can't you access view.center.x or view.center.y but you can access view.center and change it.

I wanted to write this code:

imgView_.center = scrollView_.center;
if (imgView_.frame.origin.x < 0) 
    imgView_.center = CGPointMake(imgView_.center.x + (imgView_.frame.size.width - self.view.frame.size.width) / 2, imgView_.center.y);
if (imgView_.frame.origin.y < 0)
    imgView_.center = CGPointMake(imgView_.center.x, imgView_.center.y + (imgView_.frame.size.height - self.view.frame.size.height) / 2);

As:

imgView_.center = scrollView_.center;
if (imgView_.frame.origin.x < 0) 
    imgView_.center.x = imgView_.center.x + (imgView_.frame.size.width - self.view.frame.size.width) / 2;
if (imgView_.frame.origin.y < 0)
    imgView_.center.y = imgView_.center.y + (imgView_.frame.size.height - self.view.frame.size.height) / 2;

I find the second way a lot more elegant, but I can't access the x and y, so I thought I'd ask if anyone knows what's Apple's reason for blocking it.

4

6 回答 6

8

你不必知道;这才是重点。接口对您来说很重要,而不是实现。

如果不查看该方法的来源,您将无法知道。但即使你这样做,它对你的客户来说也无关紧要。你所调用的都是List方法。

于 2013-08-22T09:08:07.923 回答
2

您可以在 getAllPos() 源代码中找到。List<Position>由于多态性将接受所有实现 List 接口的类。

于 2013-08-22T09:08:38.160 回答
1

如果您只是好奇,那么找出答案的一种方法是执行以下操作:

    List<Position> allPos = posDBM.getAllPos();
    System.out.println("The class is " + allPos.getClass().getName());

当然,你不需要知道……因为你不需要自己实例化列表实现类。数据库管理代码处理这个问题。

于 2013-08-22T10:45:00.213 回答
1

理想情况下,您不应该担心实际的实现,一旦您从方法调用返回 List,您就可以像这样迭代它。

List<Position> allPos = posDBM.getAllPos();
for(Position position : allPos){
   //Your code goes here
 }

如果您想初始化一个新列表,您可以通过使用 List 接口的不同实现以多种方式完成,现在您要选择哪种实现很大程度上取决于您的要求。

于 2013-08-22T09:12:20.020 回答
1

返回List<Position>的是泛型或Strongly Typed列表。您要询问的选项是关于ArrayList哪个指定可以占用任何对象的列表。在使用ArrayList.

于 2013-08-22T09:09:30.607 回答
0

我建议你在 posDBM.getAllPos() 之后添加一个断点并查看 allPos 变量,调试器应该告诉你类型。

于 2013-08-22T09:09:43.930 回答