<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; Array</title>
	<atom:link href="http://www.yukun.info/blog/tag/array/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP: 添字配列の初期化、出力、代入</title>
		<link>http://www.yukun.info/blog/2011/01/php-array-index.html</link>
		<comments>http://www.yukun.info/blog/2011/01/php-array-index.html#comments</comments>
		<pubDate>Mon, 03 Jan 2011 07:06:38 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1652</guid>
		<description><![CDATA[ソースコード &#60;?php // 添字配列の初期化 $numbers = array(0, 3, 100, 2087123454, ); // 数値のみ(末尾要素のカンマは省略可) $strings = array( &#8230; <a href="http://www.yukun.info/blog/2011/01/php-array-index.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2011/01/php-array-index.html">PHP: 添字配列の初期化、出力、代入</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<h3>ソースコード</h3>
<pre>
&lt;?php

// 添字配列の初期化
$numbers = array(0, 3, 100, 2087123454, ); // 数値のみ(末尾要素のカンマは省略可)
$strings = array(&#039;Jon&#039;, &#039;Mery&#039;, &#039;Sun&#039;, &#039;Ren&#039;, ); // 文字列のみ
$numstrs = array(1, 1.0, &#039;1&#039;, ); // 複数型混合(数値、少数、文字列)
$empty = array(); // 空の配列

// var_dumpで構造を確認
echo &#039;$numbers == &#039;;
var_dump($numbers);
echo &#039;$strings == &#039;;
var_dump($strings);
echo &#039;$numstrs == &#039;;
var_dump($numstrs);
echo &#039;$empty == &#039;;
var_dump($empty);
echo PHP_EOL;

// 配列の要素を出力
echo $numbers[2], PHP_EOL;
echo $strings[0], PHP_EOL;
echo $numstrs[2], PHP_EOL, PHP_EOL;

// foreach で配列の要素を先頭から順に出力
foreach ($numbers as $val) {
	echo $val, PHP_EOL;
}
echo PHP_EOL;

// 配列の要素への代入
$strings[0] = &#039;Mike&#039;;
$empty[] = &#039;Jon&#039;; // 空のブラケットを用いると要素の追加となる
echo $strings[0], PHP_EOL;
echo $empty[0], PHP_EOL;

?&gt;
</pre>
<h3>実行結果</h3>
<pre>
$numbers == array(4) {
  [0]=>
  int(0)
  [1]=>
  int(3)
  [2]=>
  int(100)
  [3]=>
  int(2087123454)
}
$strings == array(4) {
  [0]=>
  string(3) "Jon"
  [1]=>
  string(4) "Mery"
  [2]=>
  string(3) "Sun"
  [3]=>
  string(3) "Ren"
}
$numstrs == array(3) {
  [0]=>
  int(1)
  [1]=>
  float(1)
  [2]=>
  string(1) "1"
}
$empty == array(0) {
}

100
Jon
1

0
3
100
2087123454

Mike
Jon
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://www.php.net/manual/ja/language.types.array.php" target="_blank">PHP: 配列 &#8211; Manual</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/python-list.html" rel="bookmark" title="2008年6月1日">Python: リストの初期化・出力・代入・要素数</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html" rel="bookmark" title="2008年1月5日">Rubyで文字列から日本語文字をインデックス指定する</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/arraylist-capacity.html" rel="bookmark" title="2008年3月6日">ArrayListのコンストラクタに初期容量を指定することで要素の追加処理を高速化</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/php-form-submit-action-post.html" rel="bookmark" title="2008年11月8日">PHP: フォーム情報の送信・受信 &#8211; POSTメソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/10/php-set-cookie-delete-time.html" rel="bookmark" title="2008年10月31日">PHP: クライアントへのCookieの設定と削除 &#8211; setcookie()関数</a></li>
</ul>
<p><!-- Similar Posts took 11.073 ms --></p>
<p><a href="http://www.yukun.info/blog/2011/01/php-array-index.html">PHP: 添字配列の初期化、出力、代入</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2011/01/php-array-index.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ArrayListのコンストラクタに初期容量を指定することで要素の追加処理を高速化</title>
		<link>http://www.yukun.info/blog/2008/03/arraylist-capacity.html</link>
		<comments>http://www.yukun.info/blog/2008/03/arraylist-capacity.html#comments</comments>
		<pubDate>Thu, 06 Mar 2008 13:10:33 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Memory]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/20080306/arraylist%e3%81%ae%e3%82%b3%e3%83%b3%e3%82%b9%e3%83%88%e3%83%a9%e3%82%af%e3%82%bf%e3%81%ab%e5%88%9d%e6%9c%9f%e5%ae%b9%e9%87%8f%e3%82%92%e6%8c%87%e5%ae%9a%e3%81%99%e3%82%8b%e3%81%93%e3%81%a8%e3%81%a7</guid>
		<description><![CDATA[javaのArrayListのコンストラクタにはオーバーロードで幾つかの種類がありますが、その一つに以下のようなものがあります。 ArrayList(int initialCapacity) 指定された初期サイズで空のリ &#8230; <a href="http://www.yukun.info/blog/2008/03/arraylist-capacity.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/03/arraylist-capacity.html">ArrayListのコンストラクタに初期容量を指定することで要素の追加処理を高速化</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>javaのArrayListのコンストラクタにはオーバーロードで幾つかの種類がありますが、その一つに以下のようなものがあります。</p>
<blockquote><p>ArrayList(int initialCapacity)<br />
          指定された初期サイズで空のリストを作成します。<br />
<a href="http://java.sun.com/j2se/1.3/ja/docs/ja/api/java/util/ArrayList.html"><span style="font-style:italic;">Java 2 Platform SE 1.3: クラス ArrayList</span></a>
</p></blockquote>
<p>ArrayListは動的に容量を確保しますが、そのメモリ割り当て処理はCPUにそれなりの負荷を与えます。そこで、予め格納する容量が分かっていればコンストラクタの引数で初期容量を指定することで、その負荷の掛かる処理を事前に省くことが出来、その結果、処理時間の短縮につながります。</p>
<p>ちなみに、このint initialCapacityの<span style="font-weight:bold;color:#CC0000;">容量の単位は</span>コレクションの<span style="font-weight:bold;color:#CC0000;">要素数</span>です(2008/3/7修正)。</p>
<p>以下に初期容量を指定したときとしないときのでの処理時間を計るJavaのソースコードを示します。</p>
<pre>
import java.io.*;   // for BufferedReader
import java.util.*; // for ArrayList

public class DumpFile {
  /**
   * @param args : 入力ファイル名
   */
  public static void main(String[] args) {
    String line;
    ArrayList filelist = new ArrayList();
    ArrayList mlist = new ArrayList(4000000); // 計測用リスト：4898304バイトのファイル

    if (args.length < 1) { // コマンドライン引数の数
      System.out.println("How to use: java DumpFile [filename]");
      System.exit(1);
    }
    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      while ((line = br.readLine()) != null) {
        filelist.add(line); // 入力ファイルは一行=一要素として格納
        //System.out.println(line);
      }
      br.close();
    }
    catch (Exception e) {
      System.err.println(e.toString());
    }
    int n = filelist.size();
    long start = System.currentTimeMillis(); // 処理時間の計測開始
    for (int i = 0; i < n; i++) {
      //System.out.println(list.get(i));
      mlist.add(filelist.get(i));
    }
    long end = System.currentTimeMillis();
    System.out.println("実行時間:" + (end - start) + "ms");
    /*for (int i = 0; i < n; i++) {
      System.out.println(mlist.get(i));
    }*/
  }
}
</pre>
<p>入力ファイルは日本語単語405,000語で4898304Byteです。</p>
<pre>…< 中略>…
楽観
楽観さ
楽観し
楽観した
楽観したら
楽観したり
楽観したろう
楽観しちゃ
…< 中略>…</pre>
<p>結果は、</p>
<p>
<code>mlistに対して初期容量を指定しない場合：17ms<br />
mlistに対して初期容量を指定した　場合：8ms</code>
</p>
<p>となり、今回の場合は処理時間を1/2に短縮できていることが分かりました。<br />
ちなみに、ArrayListのメモリ動的割り当て容量は、現在の容量の1.5倍のようです。</p>
<blockquote><p>int newCapacity = (oldCapacity * 3)/2 + 1;<br />
<span style="font-style:italic;">ArrayList#ensureCapacityメソッド内の一文 - ArrayList.java	1.56 06/04/21</span></p></blockquote>
<h3>参考にしたサイト</h3>
<ul>
<li><a href="http://www.nextindex.net/java/collection/ArrayList.html">Java 入門 | ArrayList クラス</a></li>
</ul>
<h3>追記(2008/3/7)：</h3>
<p>1．コンストラクタでインスタンス容量を指定するものとして、StringBufferクラスがあります。これも、</p>
<pre>StringBuffer sb = new StringBuffer(40000); // 40KByteを確保</pre>
<p>のように指定します。</p>
<p>これによって、append()やinsert()によって予め割り当てられたバッファを越す際に呼び出されるメモリ動的割り当て処理を軽減することが出来ます。</p>
<p>2．ArrayListのコンストラクタに引数を指定しない場合は10個の要素を持った配列が作られます。</p>
<blockquote><p>/**<br />
 * Constructs an empty list with an initial capacity of ten.<br />
 */<br />
public ArrayList() {<br />
  this(10);<br />
}<br />
<span style="font-style:italic;">ArrayList.java	1.56 06/04/21</span></p></blockquote>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram.html" rel="bookmark" title="2008年3月7日">検索エンジンを実装 (1)転置インデックス作成</a></li>
<li><a href="http://www.yukun.info/blog/2010/05/java-networkinterface-ipv6-ipv4.html" rel="bookmark" title="2010年5月16日">Java: インターフェースとローカルのIPv6, IPv4アドレスの取得 &#8211; NetworkInterfaceクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/diff-java-ruby-string.html" rel="bookmark" title="2008年3月4日">JavaとRubyで文字列の終端の扱いの違い</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-sequence.html" rel="bookmark" title="2008年6月2日">Python: リストの抽出・連結・要素の追加</a></li>
<li><a href="http://www.yukun.info/blog/2008/10/java-inetaddress-getlocalhost.html" rel="bookmark" title="2008年10月30日">Java: ローカルホスト名とIPアドレスを取得 &#8211; InetAddress.getLocalHost()メソッド</a></li>
</ul>
<p><!-- Similar Posts took 12.826 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/03/arraylist-capacity.html">ArrayListのコンストラクタに初期容量を指定することで要素の追加処理を高速化</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/03/arraylist-capacity.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rubyで文字列から日本語文字をインデックス指定する</title>
		<link>http://www.yukun.info/blog/2008/01/ruby-string-index.html</link>
		<comments>http://www.yukun.info/blog/2008/01/ruby-string-index.html#comments</comments>
		<pubDate>Sat, 05 Jan 2008 09:23:17 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/20080105/ruby%e3%81%a7%e6%96%87%e5%ad%97%e5%88%97%e3%81%8b%e3%82%89%e6%97%a5%e6%9c%ac%e8%aa%9e%e6%96%87%e5%ad%97%e3%82%92%e3%82%a4%e3%83%b3%e3%83%87%e3%83%83%e3%82%af%e3%82%b9%e6%8c%87%e5%ae%9a%e3%81%99</guid>
		<description><![CDATA[RubyのStringインスタンスに格納されている文字列のインデックスを得るにはchrメソッドを用います。 ソースコード # chrは文字コードObjを文字列Objに変換するメソッド str1 = "abcdef" p  &#8230; <a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Rubyで文字列から日本語文字をインデックス指定する</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>RubyのStringインスタンスに格納されている文字列のインデックスを得るにはchrメソッドを用います。</p>
<h4>ソースコード</h4>
<pre>
# chrは文字コードObjを文字列Objに変換するメソッド
str1 = "abcdef"
p str1[2]     #=> 99
p str1[2].chr #=> "c"
</pre>
<p><span id="more-11"></span></p>
<h3>インデックスはバイト換算</h3>
<p>なので、2バイト長の日本語文字などは取り出せません。</p>
<pre>
str2 = "あいうえお"
p    str2[2]                   #=> 130
puts str2[2]                   #=> 130
p    str2[2].chr               #=> "\202"
puts str2[2].chr + str2[3].chr #=> "い"
</pre>
<p>そこで、</p>
<h3>日本語文字列のインデックス指定はsplitメソッドで一旦配列に分割</h3>
<pre>
str2  = "あいうえお"
jarr3 = str2.split(//s) # 文字コードがSJISの場合
puts  jarr3[3] #=> え
</pre>
<p>多バイト長の文字列処理を扱ったソースを読み込んで慣れていこうかな。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html" rel="bookmark" title="2008年8月3日">Python: リスト中の文字列を大文字⇔小文字に変換</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/extract-web-tag.html" rel="bookmark" title="2008年2月10日">Webページから指定したタグの要素を抜き出すRuby関数</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/ruby-string.html" rel="bookmark" title="2008年2月6日">Rubyで引数の設定値によって4パターンの部分文字列を取得するラッパー関数</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/diff-java-ruby-string.html" rel="bookmark" title="2008年3月4日">JavaとRubyで文字列の終端の扱いの違い</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/ruby-block.html" rel="bookmark" title="2008年1月9日">Ruby: メソッドの引数にブロックを渡す</a></li>
</ul>
<p><!-- Similar Posts took 12.948 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Rubyで文字列から日本語文字をインデックス指定する</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/01/ruby-string-index.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

