首页 \ 问答 \ 这个算法是O(1)吗?(Is this algorithm O(1)?)

这个算法是O(1)吗?(Is this algorithm O(1)?)

下面的算法是简单的O(1),还是其复杂性难以定义?

for (i = 0; i < n; ++i)
    if (i > 10)
        break;

我很困惑的事实是,当n <= 10时显然是O(n)。


Is the following algorithm simply O(1), or is its complexity trickier to define?

for (i = 0; i < n; ++i)
    if (i > 10)
        break;

I'm confused by the fact that it's obviously O(n) when n <= 10.


原文:https://stackoverflow.com/questions/25925570
更新时间:2022-07-19 06:07

最满意答案

首先,我建议您探索Struts 2 Spring集成,而不是尝试将spring集成到struts 2中。 这种集成由Strut 2 Spring插件提供。 你应该检查一下; 它允许您轻松地将bean注入struts 2操作,更不用说框架创建的所有其他对象。 这是一个链接

但是,即使基于您在代码片段中的内容,我也可以提供一些Spring基础知识的清晰度。 setter定义的bean属性必须与bean属性名称匹配。 没有这个,财产注入将无法运作。 但是,听起来你改变了字段本身的名称以匹配; 这将是现场注入。 完全不同的机制。 我建议你真的需要理解属性注入,场注入和构造函数注入的概念,以及自动布线的概念。 对不起,如果我误认为你没有拨打这个东西; 看起来你不喜欢。

但是,我建议你有更大的问题。 bean中存在依赖循环。 考虑这两个:

  <bean id="insertUserProfile" class="lotmovement.business.crud.InsertUserProfile">
        <property name="registeraction" ref="registeraction"/>
        <property name="entitystart" ref="entitystart"/>
        <property name="userprofile" ref="userprofile"/>

    </bean>

    <bean id="registeraction"  class="lotmovement.action.RegisterAction">
        <property name="insertUserProfile" ref="insertUserProfile"/>
        <property name="entitystart" ref="entitystart"/>
        <property name="recordexistuserprofile" ref="recordexistuserprofile"/>
    </bean>  

我不确定春天是否允许这样做,但是如果它确实如此,则必须做出一些关于注射如何下降的任意决定,因为它根本不可能以正常方式满足循环,我想。


First of all, I would suggest that you explore the Struts 2 Spring integration instead of trying to integrate spring into struts 2 yourself. The integration is provided by the Strut 2 Spring plugin. You should check it out; it allows you to easily inject beans into your struts 2 actions, not to mention all other objects created by the framework. Here's a link.

But, even based upon what you have in your code snippets, I can provide some clarity on a couple of Spring fundamentals. The bean property as defined by the setter must match the bean property name. Without this, property injection wouldn't work. However, it sounds like you changed the name of the field itself to match; this would be field injection. Completely different mechanisms. I suggest that you really need to understand the concepts of property injection, field injection and constructor injection, as well as the concept of auto-wiring. Sorry, if I'm misassuming that you don't have this stuff dialed; seems like you don't though.

HOWEVER, I suggest you have a bigger problem though. There is a dependency cycle in your beans. Consider these two:

  <bean id="insertUserProfile" class="lotmovement.business.crud.InsertUserProfile">
        <property name="registeraction" ref="registeraction"/>
        <property name="entitystart" ref="entitystart"/>
        <property name="userprofile" ref="userprofile"/>

    </bean>

    <bean id="registeraction"  class="lotmovement.action.RegisterAction">
        <property name="insertUserProfile" ref="insertUserProfile"/>
        <property name="entitystart" ref="entitystart"/>
        <property name="recordexistuserprofile" ref="recordexistuserprofile"/>
    </bean>  

I don't know for sure whether spring allows this, but if it did it would have to make some arbitrary decision about how the injections go down as it's simply not possible satisfy a cycle in a normal fashion, I think.

相关问答

更多
  • 首先,我建议您探索Struts 2 Spring集成,而不是尝试将spring集成到struts 2中。 这种集成由Strut 2 Spring插件提供。 你应该检查一下; 它允许您轻松地将bean注入struts 2操作,更不用说框架创建的所有其他对象。 这是一个链接 。 但是,即使基于您在代码片段中的内容,我也可以提供一些Spring基础知识的清晰度。 setter定义的bean属性必须与bean属性名称匹配。 没有这个,财产注入将无法运作。 但是,听起来你改变了字段本身的名称以匹配; 这将是现场注入。 ...
  • 问题是您在执行操作时正在重新定义应用程序的主模块 angular.module('inspinia', ['angularCharts']) 因此模块丢失了之前添加的所有组件及其所有依赖项(ui-router等)。 相反,您必须修改模块的axisting定义以添加对angularCharts的依赖,并且在您的控制器中,只需检索先前定义的模块: angular.module('inspinia').controller(...); The problem is that you're redefining ...
  • 当错误通知时,javax.persistence.EntityManager中没有getMetaModel()方法。 检查JPA 1.0和JPA 2.0的来源。 EntityManager JPA 2.0 EntityManager JPA 1.0 此方法仅存在于2.0版中。 在我看来,如果版本1.0中没有jar的JPA,你应该仔细检查你的依赖项 As the error informs, there is no method getMetaModel() in javax.persistence.Enti ...
  • 你是对的,事务性数据(例如:代表表格行的数据)通常不会被声明性地注入。 Spring DI(依赖注入)通常用于处理多个类之间的协作。 我见过的常见示例是DAO(数据访问对象)和MVC(模型视图控制器)模式。 在企业环境中,通常有一个有数十或数百个数据库表的项目 - 因此有数十个/数百个DAO类被注入到控制器类中。 如果你不使用DI,你需要有意识地管理首先应该创建哪个DAO,以及哪个DAO应该注入哪个控制器等等(这是一场噩梦) 代码重构也是(应该)是一件常见的事情。 业务需求总是在不断变化。 如果没有DI,一 ...
  • 与Servlet一样,控制器的生命周期超出了请求范围。 应用程序启动时,应用程序中的所有控制器仅实例化一次; 之后,这些对象被重新用于服务所有请求。 正如Bozho指出的那样,默认情况下所有bean都是单例范围,因此它们将在任何地方重复使用,除非另有说明。 Like Servlets, Controllers' lifecycle spans beyond requests. All of controllers in the application are instantiated only once w ...
  • 你似乎有两个问题,不确定哪一个最能影响你。 你错过了一个ExceptionMapper ,它应该将你后端的异常类型映射到HTTP响应。 无论代码在REST容器中运行,您都会收到NullPointerException 。 也许如果你俯视这个NullPointerException的堆栈跟踪,你可以得到一个关于出错的提示。 You seem to have two problems, not sure which one affects you most. You're missing an Exceptio ...
  • 不要将用于此用例。 这实际上是MapFactoryBean的别名。 使用经典的元素,它应该工作。 Don't use for this use-case. That is actually an alias for a MapFactoryBean. Use the classic element instead and it should work.
  • 你应该可以使用最新的Spring 4 RC1来做到这一点。 之前的版本根本不支持通用注入。 有关详细信息,请参阅此票证和相关提交。 You should be able to do this using the latest Spring 4 RC1. Versions before that do not support generic injection at all. See this ticket and related commits for details.
  • 嗯,是的,这肯定会失败,因为你使用的构造函数没有参数: OrderService objOrdServ = new OrderService(); 因此_iordProc在以这种方式使用时将始终为null。 传递参数,或使用将为您进行实例化的IoC框架。 var objOrdServ = new OrderService(new OrderProcessingEngine()); IoC通常使用其他框架完成,例如AutoFac或Unity ,仅举几例。 Well, yes, that will defi ...
  • 你应该像这里描述的那样创建cli.php文件(cli bootstrap)。 然后你应该创建你在这里描述的任务类。 然后你可以运行它: php cli.php task_name action_name You should create cli.php file (cli bootstrap) like described here. Then you should create your tasks classes like described here. And then you can run i ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)