スキップしてメイン コンテンツに移動

投稿

C#: Convert 1 Dimensional Array to 2 Dimensional Jagged Array

Code using System; using System.Collections.Generic; using System.Linq; namespace Utility { public static class ArrayUtils { public static T[][] To2DimensionalJaggedArray<T>(this T[] source, int block) { var res = new T[block][]; int size = source.Length / block; for (int i = 0; i < block; i++) { res[i] = new T[size]; Array.Copy(source, i*size, res, 0, size); // advantage of jagged array is that you can use Array.copy method etc. } return res; } } } Example Usage int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[][] array2 = array1.To2DimensionalJaggedArray(3); /* array2 will be int[3][] and each elements will be... [0] = { 1, 2, 3 }, [1] = { 4, 5, 6 }, [2] = { 7, 8, 9 } */

The Simplest Setup to Use Amazon Product API in Java

In this post, I will explain the simplest setup to use Amazon Product API in Java. I know Amazon provides soap interface and we can automatically create Soap code for accessing Amazon Product API. But sometimes, it is too much for light users. So I will show you the simplest way to use Amazon Product API using SignedRequestsHelper. Hope this post helps your application development ;) Preparation Create an AWS Account Get AWS Key, secret key and associate tag: I think you can create them from http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html Get SignedRequestsHelper: You can download from https://code.google.com/p/amazon-product-advertising-api-sample/source/browse/src/com/amazon/advertising/api/sample/SignedRequestsHelper.java Java Code Example Congratulation! After you finish above preparation, now you can request Amazon Product API!! The following code shows how to use SignedRequestsHelper. The key points of the examp

Switch Html View Based on Devices Using Filter

Introduction In this post, I will explain how to switch view based on devices on the web application - like pc, smartphone, tablet. The basic strategy is checking user agenet which is sent from these devices, and building the specific html for the device. The key issue uis how to check user agent of all coming http requests efficiently and ellegantly. If you use some web application framework such as Java Spring Framework or PHP Symfony framework, the solution is using &qout;Filter&qout; which every http requests pass through. Let me show you the example implementation using Spring framework. Basic Strategy In filter check if "view_mode" in user cookies, set the view mode based on that cookie value if the cookie does not exist, set view mode based on user agent and publish corresponding view mode cookie set the selected view mode to HttpServletRequest attribute. In controller, set view template based on view mode in the HttpServletRequest attribute

Encode Java Object to XML and Decode XML to Java Object

In current software development world, serializing object to some human readable format such as "XML", "JSON" is quite common. In this post I will show you a small code snippet for encoding Java Object to XML and decoding XML to Java Object only using Java Standard API. I think this approach is suitable if you need to quick and simple solution to encoding and decoding java object to XML string. Actually code is very simple. Please see the code below :) If you need to input or output XML to file, you should pass FileOutputStream or FileInputStream instead of ByteArrayOutputStream or ByteArrayInputStream. package com.dukesoftware.utils.xml; import java.beans.XMLDecoder; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class XMLUtils { public final static Object decodeToXML(String file) throws FileNotFoundException{ try(XMLDecoder decoder =

Java: Joining String Array with Separator (Equilvarent to PHP implode)

If you would like to join array with a separator, what will you do? e.g. Joining "a","b","c" with separator "," to "a,b,c". In PHP, you just use implode function. Actually PHP has a lot of functions like manipulating, converting string and array. In java, these kind of functions are not provided in official JDK. If you are familiar with Java Libraries, you just use them. - Joiner in google Guava, StringUtils in Apache Coommons. I will show you quick code snippet for jonining or imploding java string array with a separator. private static String join(String[] pieces, String separator) { if(pieces.length == 0) return ""; StringBuilder sb = new StringBuilder(pieces[0]); for(int i = 1; i < pieces.length; i++) { sb.append(separator).append(pieces[i]); } return sb.toString(); } If the number of array elements is zero, the method returns empty string, but if you would not prefer this

Visual Studio 2010 SP1のアンインストール

Visual Studio 2013に乗り換えるためにVisual Studio 2010をアンインストールしようとしたところで問題発生。。。 先にVisual Studio 2010本体をアンインストールした後、Visual Studio 2010 SP1をアンインストールできなくて困っていました。 Google先生で調べたところ、以下の情報が見つかり、書かれていた通り実施したところ無事Visual Studio 2010 SP1のアンインストールに成功しました。 How to uninstall/remove Visual Studio SP1 アンインストール手順は以下の通りです。 http://www.microsoft.com/en-gb/download/details.aspx?id=23691 からMicrosoft Visual Studio 2010 Service Pack 1 (Installer)をダウンロード VS10sp1-KB983509.exeというファイル名でダウンロードされる(はず)。 コマンドプロンプトから以下のコマンドを実行 (以下の例は、c:\tempにVS10sp1-KB983509.exeがある場合) c:\temp\VS10sp1-KB983509.exe /uninstall /force ダイアログが立ち上がるので、アンインストールを選択して次へ進めばOK!

Treat XPath Only Using Library Provided by Java SDK

Have you ever treated XML only using library provided in Java SDK? I think many people use external library for treating XML in Java. To be honest, I would prefer to use JDom ;P But I think it's good thing to know how to treat XML only using Java SDK. I will show you small code snippet for treating XML especially from XPath usage. The classes you should remember for treating XML is: DocumentBuilderFactory DocumentBuilder Document XPathFactory XPath XPathExpression XPathConstants NodeList Node Oops, a bit quite too much. Anyway I will show you an example code for reading XML string and select nodes by XPath. package com.dulesoftware.xpath; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax