我想创建一个新的对象数组,将两个较小的数组放在一起.
它们不能为空,但大小可能为0.
我无法在这两种方式之间进行选择:它们是等效还是更高效(例如system.arraycopy()复制整个块)?
MyObject[] things = new MyObject[publicThings.length+privateThings.length];
System.arraycopy(publicThings, 0, things, 0, publicThings.length);
System.arraycopy(privateThings, 0, things, publicThings.length, privateThings.length);
Run Code Online (Sandbox Code Playgroud)
要么
MyObject[] things = new MyObject[publicThings.length+privateThings.length];
for (int i = 0; i < things.length; i++) {
if (i<publicThings.length){
things[i] = publicThings[i]
} else {
things[i] = privateThings[i-publicThings.length]
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的区别是代码的外观?
编辑:感谢链接的问题,但他们似乎有一个未解决的讨论:
如果it is not for native types:byte [],Object [],char [] 真的更快吗?在所有其他情况下,执行类型检查,这将是我的情况,因此将是等效的......不是吗?
在另一个相关问题上,他们说the size matters a lot,对于size> 24,system.arraycopy()获胜,小于10,手动for循环更好......
现在我真的很困惑.
我想知道是否有人可以向我解释以下奇怪之处.我正在使用Java 8更新11.
鉴于这种方法
private <F,T> T runFun(Function<Optional<F>, T> fun, Optional<F> opt) {
return fun.apply(opt) ;
}
Run Code Online (Sandbox Code Playgroud)
如果我首先构造一个函数Object,并将其传递给上面的方法,那么事情就会编译.
private void doesCompile() {
Function<Optional<String>, String> fun = o -> o.orElseThrow(() -> new RuntimeException("nah"));
runFun(fun, Optional.of("foo"));
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将函数内联为lambda,编译器会说
未报告的例外X; 必须被抓住或宣布被抛出
private void doesNotCompile () {
runFun(o -> o.orElseThrow(() -> new RuntimeException("nah")), Optional.of("foo"));
}
Run Code Online (Sandbox Code Playgroud)
更新:原来错误消息由maven缩写.如果直接用javac编译,则错误是:
error: unreported exception X; must be caught or declared to be thrown
runFun(o -> o.orElseThrow(() -> new RuntimeException("nah")), Optional.of("foo"));
^
where X,T are type-variables:
X extends Throwable declared …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的eclipse环境更新为java 8.我安装了jdk和jre版本8.我也这样做了:
https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler
然后在项目 - >属性 - > java构建路径 - > JRE系统库 - >编辑 - >执行环境 - > JavaSE-1.8(未绑定)这是唯一一个在其名称中包含java 8的选项.但是当我选择它时它会说:工作空间中没有与指定执行环境兼容的JRE:JavaSE-1.8
我正在尝试创建动态表单,用户可以根据需要添加动态文本字段.这是我的jquery代码..
$(document).ready(function() {
$("#add").click(function() {
var intId = $("#buildyourform div").length +1;
var fieldWrapper = $("<div class=\"fieldwrapper\" name=\"field" + intId + "\" id=\"field" + intId + "\"/>");
var fName = $("<input type=\"text\" name=\"name\" class=\"fieldname\" id=\"tb"+ intId +"_1\"/>");
var lname = $("<input type=\"text\" name=\"email\" class=\"lastname\" id=\"tb"+ intId +"_2\"/>");
var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
var addButton = $("<input type=\"button\" class=\"add\" id=\"add\" value=\"+\" />")
removeButton.click(function() {
$(this).parent().remove();
});
fieldWrapper.append(fName);
fieldWrapper.append(lname);
fieldWrapper.append(removeButton);
fieldWrapper.append(addButton);
$(this).remove();
$("#buildyourform").append(fieldWrapper);
});
});
Run Code Online (Sandbox Code Playgroud)
和Html代码是......
<fieldset id="buildyourform">
<legend>Build your …Run Code Online (Sandbox Code Playgroud) 我试图使用jsp:include标签将DTO对象从一个jsp发送到另一个jsp.但它始终将其视为字符串.我无法在包含的jsp文件中使用DTO.
这是一个代码..
<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">
<jsp:include page="attributeSubFeatureRemove.jsp" >
<jsp:param name="attribute" value="${attribute}" />
</jsp:include>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
attributeSubFeatureRemove.jsp文件..
<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">
<c:forEach items="${subAttribute.attributeValues}" var="subValue">
<c:if test="${ subValue.preSelectionRequired}">
<c:set var="replaceParams" value=":${subAttribute.name}:${subValue.name}" />
<c:set var="removeURL" value="${fn:replace(removeURL, replaceParams, '')}" />
</c:if>
</c:forEach>
<jsp:include page="attributeSubFeatureRemove.jsp">
<jsp:param name="subAttribute" value="${subAttribute}" />
</jsp:include>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
这里我试图从param获取属性值,它总是发送String Type Value.有没有办法在attributeSubFeatureRemove jsp文件中发送Object(DTO)?请帮忙.
我正在使用omniauth-twitter gem在我的rails应用程序中启用Twitter登录.这是我的代码......
gemfile -
gem 'omniauth', '~> 1.1.1'
gem 'omniauth-twitter'
Run Code Online (Sandbox Code Playgroud)
routes.rb -
match '/auth/twitter/callback', to: 'users#twitter_login'
match 'auth/failure', to: 'static_pages#home'
Run Code Online (Sandbox Code Playgroud)
User_controller.rb -
def twitter_login
auth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(auth['provider'],auth['uid'])
if authentication
sign_in authentication.user
redirect_to root_url
else
if(User.where(:email => auth['extra']['raw_info']['email']).exists?)
flash[:notice] = "You already have account in ibetter"
redirect_to root_url
else
user = User.new
user.apply_omniauth(auth)
if user.save(:validate => false)
sign_in user
flash[:notice] = "Welcome to Ginfy"
redirect_to root_url
else
flash[:error] = "Error while creating a user account. Please try …Run Code Online (Sandbox Code Playgroud) 我是Grails的新手.我使用Spring Security Grails插件进行身份验证.我想在我的视图gsp文件中获取当前用户.
我这样想...
<g:if test="${post.author == Person.get(springSecurityService.principal.id).id }">
<g:link controller="post" action="edit" id="${post.id}">
Edit this post
</g:link>
</g:if>
Run Code Online (Sandbox Code Playgroud)
在这里,我想显示编辑此帖子链接,仅显示由signed_in用户创建的帖子.但它显示出错误 -
Error 500: Internal Server Error
URI
/groovypublish/post/list
Class
java.lang.NullPointerException
Message
Cannot get property 'principal' on null object
Run Code Online (Sandbox Code Playgroud)
这是我的Post.groovy -
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
Person author
....... more code ....
Run Code Online (Sandbox Code Playgroud)
这是我的Person.groovy域类文件 -
class Person {
transient springSecurityService
String realName
String …Run Code Online (Sandbox Code Playgroud) 有没有办法通过在生产环境中意外初始化系统来锁定Hybris.如果有人错误地点击初始化按钮,那就太冒险了.
我正在测试一些Java8流API代码,但我无法弄清楚这个代码发生了什么.
我在考虑ParallelStream以及它是如何工作的,我进行了一些比较.两个不同的方法进行大迭代,添加32.768.000个BigDecimals,一个使用ParallelStream,另一个使用正常迭代.我参加了一项测试,我知道它无效,但有些事情引起了我的注意.
测试是:
并行流:
private static void sumWithParallelStream() {
BigDecimal[] list = new BigDecimal[32_768_000];
BigDecimal total = BigDecimal.ZERO;
for (int i = 0; i < 32_768_000; i++) {
list[i] = new BigDecimal(i);
}
total = Arrays.asList(list).parallelStream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Total: " + total);
}
Run Code Online (Sandbox Code Playgroud)
普通代码:
private static void sequenceSum() {
BigDecimal total = BigDecimal.ZERO;
for (int i = 0; i < 32_768_000; i++) {
total = total.add(new BigDecimal(i));
}
System.out.println("Total: " + total);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Total: 536870895616000
sumWithParallelStream(): 30502 ms
Total: 536870895616000 …Run Code Online (Sandbox Code Playgroud) 我正在Hybris项目的门面层面编写测试用例.我正在创建模型实例并设置名称和代码.模型有一些属性本地化,因为我得到了no LocaleProvider异常.
java.lang.IllegalStateException: there is no LocaleProvider for (detached) model de.hybris.platform.servicelayer.model.ItemModelContextImpl@66c677a7
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocaleProvider(ItemModelContextImpl.java:481)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getCurrentLocale(ItemModelContextImpl.java:469)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.toDataLocale(ItemModelContextImpl.java:406)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocalizedValue(ItemModelContextImpl.java:323)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:227)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:217)
Run Code Online (Sandbox Code Playgroud)
这是一个测试类
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Locale.class, Config.class })
public class HCCB2BClassificationFacadeUnitTest {
@InjectMocks
private final HCCB2BClassificationFacade hccb2bClassificationFacade = new HCCB2BClassificationFacadeImpl();
@Mock
HCCB2BClassificationService hccb2bClassificationService = new HCCB2BClassificationServiceImpl();
@Mock
private SessionService sessionService;
@Mock
private HCCB2BClassificationDAO hccb2bClassificationDAO;
@Mock
private SearchRestrictionService searchRestrictionService;
@Before
public void setUp() throws SystemException {
MockitoAnnotations.initMocks(this);
ClassAttributeAssignmentModel classAttributeAssignmentModel = new ClassAttributeAssignmentModel();
ClassificationAttributeModel classificationAttributeModel = new ClassificationAttributeModel();
classificationAttributeModel.setCode("Procedure"); …Run Code Online (Sandbox Code Playgroud) java ×5
java-8 ×3
html ×2
hybris ×2
eclipse ×1
grails ×1
grails-2.0 ×1
gsp ×1
java-stream ×1
javascript ×1
jquery ×1
jsp ×1
jsp-tags ×1
jstl ×1
lambda ×1
mocking ×1
mockito ×1
omniauth ×1
performance ×1
powermock ×1
powermockito ×1
ruby ×1
twitter ×1
upgrade ×1