.toArray(new MyClass [0])或.toArray(new MyClass [myList.size()])?(.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?)
假设我有一个ArrayList
ArrayList<MyClass> myList;
而且我想调用toArray,有没有一个表现的理由使用
MyClass[] arr = myList.toArray(new MyClass[myList.size()]);
过度
MyClass[] arr = myList.toArray(new MyClass[0]);
?
我更喜欢第二种风格,因为它不那么冗长,我假设编译器将确保空数组不会真正被创建,但是我一直在想这是否是真的。
当然,在99%的情况下,它并没有改变一种方式,但是我希望保持一般的代码与我的优化内部循环之间的一致风格...
Assuming I have an ArrayList
ArrayList<MyClass> myList;
And I want to call toArray, is there a performance reason to use
MyClass[] arr = myList.toArray(new MyClass[myList.size()]);
over
MyClass[] arr = myList.toArray(new MyClass[0]);
?
I prefer the second style, since it's less verbose, and I assumed that the compiler will make sure the empty array doesn't really get created, but I've been wondering if that's true.
Of course, in 99% of the cases it doesn't make a difference one way or the other, but I'd like to keep a consistent style between my normal code and my optimized inner loops...
原文:https://stackoverflow.com/questions/174093
最满意答案
使用Eloquent或查询构建器,在Laravel中进行批量插入是非常容易的。
您可以使用以下方法。
$data = array( array('user_id'=>'Coder 1', 'subject_id'=> 4096), array('user_id'=>'Coder 2', 'subject_id'=> 2048), //... ); Model::insert($data); // Eloquent DB::table('table')->insert($data); // Query Builder
在您的情况下,您已经有$查询变量中的数据。
It is really easy to do a bulk insert in Laravel using Eloquent or the query builder.
You can use the following approach.
$data = array( array('user_id'=>'Coder 1', 'subject_id'=> 4096), array('user_id'=>'Coder 2', 'subject_id'=> 2048), //... ); Model::insert($data); // Eloquent approach DB::table('table')->insert($data); // Query Builder approach
In your case you already have the data within the
$query
variable.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在你的模型中,你可以这样做: $db = $this->getDBO(); $query = " INSERT INTO x (a,b) VALUES ('1', 'one'), ('2', 'two'), ('3', 'three') "; $db->setQuery($query); $db->query(); 如果你在你的模型之外,你需要像这样获取DB对象: $db = JFactory::getDBO(); In your model you can do this: $d ...
-
使用关键字VALUES来引用新值(参见文档 )。 INSERT INTO beautiful (name, age) VALUES ('Helen', 24), ('Katrina', 21), ('Samia', 22), ('Hui Ling', 25), ('Yumie', 29) ON DUPLICATE KEY UPDATE age = VALUES(age), ... Use keyword VALUES to refer to ...
-
尝试这个: insert into my_table(contents, css) values ('some text', 'color: blue'); // grab id from that SELECT LAST_INSERT_ID(); insert into my_table(contents, css, parent_id) values ('child text', 'color: pink', last_insert_id); SELECT LAST_INSERT_ID(); ...
-
如何使用雄辩/流利的方式从单个查询中插入多行(How to insert multiple rows from a single query using eloquent/fluent)[2022-09-16]
使用Eloquent或查询构建器,在Laravel中进行批量插入是非常容易的。 您可以使用以下方法。 $data = array( array('user_id'=>'Coder 1', 'subject_id'=> 4096), array('user_id'=>'Coder 2', 'subject_id'=> 2048), //... ); Model::insert($data); // Eloquent DB::table('table')->insert($data); ... -
使用PDO准备语句插入多个值 在一个执行语句中插入多个值。 为什么因为根据这个页面比普通的插入更快。 http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html $datafields = array('fielda', 'fieldb', ... ); $data[] = array('fielda' => 'value', 'fieldb' => 'value' ....); $data[] = array('fielda' => 'value', ...
-
Do this $values = "('" . $e_mail . "', '" .$pwd . "', '" .$salt . "', '" . $id_application . "')"; Do this $values = "('" . $e_mail . "', '" .$pwd . "', '" .$salt . "', '" . $id_application . "')";
-
在PostgreSQL 8.4 : 使用generate_series(start, stop, step interval)您可以填充每月插入数据的日期: SELECT t.d::date, date as original_date, user_id FROM user_activity, generate_series(date,date+ interval '1 month - 1 day','1 day') t(d) 请查看此链接以获取更多信息generate_series 在PostgreSQ ...
-
我可以使用eloquent / fluent以最大ID更新单行(Can I update a single row by maximum id using eloquent/fluent)[2023-04-25]
您可以获取最后一行(基于created_at字段): $row = Notification::where('from_user_id', '=', Auth::id()) ->where('to_user_id', 0) ->orderBy('created_at', 'desc') ->first(); 并更新该to_user_id上的to_user_id : $row->to_user_id = YOUR ... -
您可以保留所有插入,然后执行第二次插入,将表格融合为您想要的格式。 特别: insert into final (item_id, parameter_id, value) ( select item_id, 1, category from parameters union all select item_id, 2, color from parameters union all ... ) 将这两个步骤打包到单个事务中。 You can keep the insert you have, and t ...