Javaã使ããªãå¿ ãè¦ãã¦ãããããã¼ã¿æ§é - é åã»ãªã¹ãã»ããã
Javaã使ãå ´åã«å¿ ãå¿ è¦ã«ãªããã¼ã¿æ§é - é åã»ãªã¹ãã»ãããã®ä½¿ãæ¹ã«ã¤ãã¦ç°¡åã«èª¬æãã¾ãã
ç®æ¬¡
é å
é
åã¯è¤æ°ä»¶ã®ãã¼ã¿ãæ±ãããã®ãã¼ã¿æ§é ã§ãã
Javaã®é
åã®ç¹å¾´ã¯ãçææã«ãµã¤ãº(æ ¼ç´ãããã¼ã¿ã®åæ°)ã決ããå¿
è¦ããããã¨ã§ãããã£ããé
åãçæããã¨ãå¾ããé
åã®ãµã¤ãºãå¤æ´ãããã¨ã¯ã§ãã¾ããã
â é åãçæããã
é åã®çæã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ãã¼ã¿å[] é åã®åå = new ãã¼ã¿å[é åã®ãµã¤ãº];
é åã®çæã§ã¯ãæ ¼ç´ãããã¼ã¿ã®åã¨é åã®ååã¨é åã«æ ¼ç´ãããã¼ã¿ã®åæ°(é åã®ãµã¤ãº)ãæå®ãã¾ãã
ä¾ãã°ã10åã®Stringãæ ¼ç´ããé åarrayãçæããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
String[] array = new String[10];
â é åã«ãã¼ã¿ãæ ¼ç´ããã
é åã¸ã®ãã¼ã¿ãæ ¼ç´ã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
é åã®åå[ã¤ã³ããã¯ã¹] = ãã¼ã¿;
é
åã¸ã®ãã¼ã¿ã®æ ¼ç´ã§ã¯ãé
åã®ååã¨ã¤ã³ããã¯ã¹ã¨æ ¼ç´ãããã¼ã¿ãæå®ãã¾ãã
ã¤ã³ããã¯ã¹ã¨ã¯é
åä¸ã®ä½ç½®ã表ãæ°åã§ãã0ã以ä¸ãé
åã®ãµã¤ãºãããå°ããã®æ°åãæå®ããå¿
è¦ãããã¾ãã
ä¾ãã°ãé åarrayã¸ã®ãã¼ã¿ã®æ ¼ç´ã¯ä»¥ä¸ã®ããã«ãªãã¾ãããã®ä¾ã§ã¯ãé åã®ãµã¤ãºã10ãªã®ã§ãã¤ã³ããã¯ã¹ã¯0以ä¸ã§9ããå°ãããªããã°ãªãã¾ããã
array[0] = "hello"; array[1] = "world"; array[9] = "test";
â é åãããã¼ã¿ãåå¾ããã
é åããã®ãã¼ã¿ã®åå¾ã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ã¡ã½ãã( é åã®åå[ã¤ã³ããã¯ã¹] ); å¤æ° = é åã®åå[ã¤ã³ããã¯ã¹]; return é åã®åå[ã¤ã³ããã¯ã¹];
é
åããã®ãã¼ã¿ã®åå¾ã§ã¯ãé
åã®ååã¨ãã¼ã¿ãåå¾ããä½ç½®(ã¤ã³ããã¯ã¹)ãæå®ãã¾ããã¤ã³ããã¯ã¹ã®èãæ¹ã¯ããã¼ã¿ã®æ ¼ç´æã¨åæ§ã§ãã
é
åããåå¾ãããã¼ã¿ã¯ãå¤æ°ã«ä»£å
¥ããããã¡ã½ããã®å¼æ°ã«æ¸¡ããããã¡ã½ããã®æ»ãå¤ã¨ãã¦è¿ãããã§ãã¾ãã
ä¾ãã°ãé åarrayã®ãã¼ã¿ãåå¾ãã¦ç»é¢ã«è¡¨ç¤ºããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
System.out.println( array[0] ); => hello System.out.println( array[5] ); => null System.out.println( array[9] ); => test
â é åä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãããã
é åä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãããããã«ã¯ã以ä¸ã®ãããªforæã使ç¨ãã¾ãã
for ( int i = 0; i < é åã®åå.length; ++i ) { é åã®åå[i] = ãã¼ã¿; } for ( int i = 0; i < é åã®åå.length; ++i ) { ã¡ã½ãã( é åã®åå[i] ); }
ãã®foræã®ãé åã®åå.lengthãã¯é åã®ãµã¤ãºã表ãã¾ãããã®ããããã®foræã使ãã°é åä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãè¡ãã¾ãã
ä¾ãã°ããµã¤ãº10ã®é åarrayã«10åã®ãã¼ã¿ãæ ¼ç´ããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
for ( int i = 0; i < array.length; ++i ) { array[i] = "ãã¼ã¿_" + i; }
ããã¯ä»¥ä¸ã®ããã«æ¸ããå ´åã¨åãçµæã«ãªãã¾ãã
array[0] = "ãã¼ã¿_0"; array[1] = "ãã¼ã¿_1"; array[2] = "ãã¼ã¿_2"; array[3] = "ãã¼ã¿_3"; array[4] = "ãã¼ã¿_4"; array[5] = "ãã¼ã¿_5"; array[6] = "ãã¼ã¿_6"; array[7] = "ãã¼ã¿_7"; array[8] = "ãã¼ã¿_8"; array[9] = "ãã¼ã¿_9";
ããä¸ã¤ä¾ãæãã¾ããé åarrayä¸ã®ãã¼ã¿ããã¹ã¦è¡¨ç¤ºããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
for ( int i = 0; i < array.length; ++i ) { System.out.println( array[i] ); }
ãªã¹ã
ãªã¹ããã¾ãé
åã¨åæ§ã«ãè¤æ°ä»¶ã®ãã¼ã¿ãæ±ãããã®ãã¼ã¿æ§é ã§ãã
ãã ããé
åã¨ç°ãªãããªã¹ãã®çææã«ãµã¤ãºã決ããå¿
è¦ã¯ããã¾ããããªã¹ãã«ãã¼ã¿ã追å ãã度ã«ããªã¹ãã®ãµã¤ãºã¯å¢ãã¦ããã¾ãã
(注) ãªã¹ãã使ãã«ã¯importæãå¿ è¦
ãªã¹ãã使ãã«ã¯ãã½ã¼ã¹ãã¡ã¤ã«ã®æåã®æ¹ã«ä»¥ä¸ã®importæãå¿ è¦ã§ãã
import java.util.*;
â ãªã¹ããçæããã
ãªã¹ãã®çæã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
List<ãã¼ã¿å> ãªã¹ãã®åå = new ArrayList<ãã¼ã¿å>();
ãªã¹ãã®çæã§ã¯ãæ ¼ç´ãããã¼ã¿ã®åã¨ãªã¹ãã®ååãæå®ãã¾ãããµã¤ãºã®æå®ã¯å¿ é ã§ã¯ããã¾ããã
ä¾ãã°ãStringãæ ¼ç´ãããªã¹ãlistãçæããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
Listlist = new ArrayList ();
â ãªã¹ãã«ãã¼ã¿ã追å ããã
ãªã¹ãã¸ã®ãã¼ã¿ã®è¿½å ã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ãªã¹ãã®åå.add( ãã¼ã¿ );
ãªã¹ãã¸ã®ãã¼ã¿ã®è¿½å ã§ã¯ããªã¹ãã®ååã¨è¿½å ãããã¼ã¿ãæå®ãã¾ãã
追å ã®ãã³ã«ããã¼ã¿ã¯ãªã¹ãã®æ«å°¾ã«è¿½å ããããªã¹ãã®ãµã¤ãºã1å¢ãã¾ãã
ä¾ãã°ããªã¹ãlistã«ãã¼ã¿ã追å ããã«ã¯ä»¥ä¸ã®ããã«æ¸ãã¾ãã
list.add( "work" ); list.add( "play" ); list.add( "sleep" );
â ãªã¹ããããã¼ã¿ãåå¾ããã
ãªã¹ãããã®ãã¼ã¿ã®åå¾ã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ã¡ã½ãã( list.get( ã¤ã³ããã¯ã¹ ) ); å¤æ° = list.get( ã¤ã³ããã¯ã¹ ); return list.get( ã¤ã³ããã¯ã¹ );
ãªã¹ãããã®ãã¼ã¿ã®åå¾ã§ã¯ããªã¹ãã®ååã¨ãã¼ã¿ãåå¾ããä½ç½®(ã¤ã³ããã¯ã¹)ãæå®ãã¾ãã
ã¤ã³ããã¯ã¹ã¨ã¯ãªã¹ãä¸ã®ä½ç½®ã表ãæ°åã§ãã0ã以ä¸ããªã¹ãã®ãµã¤ãºãããå°ããã®æ°åãæå®ããå¿
è¦ãããã¾ãã
ãªã¹ãããåå¾ãããã¼ã¿ã¯ãå¤æ°ã«ä»£å
¥ããããã¡ã½ããã®å¼æ°ã«æ¸¡ããããã¡ã½ããã®æ»ãå¤ã¨ãã¦è¿ãããã§ãã¾ãã
ä¾ãã°ããªã¹ãlistã®ãã¼ã¿ãåå¾ãã¦ç»é¢ã«è¡¨ç¤ºããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
System.out.println( list.get( 0 ) ); => work System.out.println( list.get( 1 ) ); => play System.out.println( list.get( 2 ) ); => sleep
â ãªã¹ãã®ãã¼ã¿ãç½®ãæããã
ãªã¹ãã®ãã¼ã¿ã®ç½®ãæãã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ãªã¹ãã®åå.set( ã¤ã³ããã¯ã¹, æ°ãããã¼ã¿ );
ãªã¹ãã®ãã¼ã¿ã®ç½®ãæãã§ã¯ããªã¹ãã®ååã¨ãã¼ã¿ãç½®ãæããä½ç½®(ã¤ã³ããã¯ã¹)ãæ°ãããã¼ã¿ãæå®ãã¾ããã¤ã³ããã¯ã¹ã®èãæ¹ã¯ãã¼ã¿ã®åå¾ã¨åæ§ã§ãã
ç½®ãæããè¡ãã¨ãã¤ã³ããã¯ã¹ã使ã£ã¦åå¾ã§ãããã¼ã¿ããæ°ãããã¼ã¿ã«å¤ããã¾ãã
ä¾ãã°ããªã¹ãlistã®ãã¼ã¿ãç½®ãæããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
list.set( 1, "walk" )
System.out.println( list.get( 1 ) ); => walk
â ãªã¹ãä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãããã
ãªã¹ãä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãããããã«ã¯ã以ä¸ã®ãããªforæã使ç¨ãã¾ãã
for ( int i = 0; ãªã¹ãã®åå.size(); ++i ) { ã¡ã½ãã( ãªã¹ãã®åå[i] ); }
ãã®foræã®ããªã¹ãã®åå.size()ãã¯ãªã¹ãã®ãµã¤ãºã表ãã¾ãããã®ããããã®foræã使ãã°ãªã¹ãä¸ã®ãã¹ã¦ã®ãã¼ã¿ã«åãå¦çãè¡ãã¾ãã
ä¾ãã°ããªã¹ãlistä¸ã®ãã¼ã¿ããã¹ã¦è¡¨ç¤ºããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
for ( int i = 0; i < list.size(); ++i ) { System.out.println( list.get( i ) ); }
ããã¯ããªã¹ãlistã®ãµã¤ãºã3ã ã¨ããã°ã以ä¸ã®ããã«æ¸ããå ´åã¨åãçµæã«ãªãã¾ãã
System.out.println( list.get( 0 ) ); => hello System.out.println( list.get( 1 ) ); => walk System.out.println( list.get( 2 ) ); => sleep
ããã
ãããã¨ã¯ããã¼ã«é¢é£ä»ãã¦ãã¼ã¿ãæ ¼ç´ãããã¼ã¿æ§é ã§ãã
ãããã¸ã®ãã¼ã¿ã®æ ¼ç´æã«ããã¼ã¸ã®é¢é£ä»ããè¡ãã¾ããããã¦ãããããããã¼ã¿åå¾æã«ãé¢é£ã¥ãããã¼ã使ç¨ãã¾ãã
説æãèªããããå®éã«åãããæ¹ãåãããããã¨æãã¾ãã
(注) ãããã使ãã«ã¯importæãå¿ è¦
ãããã使ãå ´åããã½ã¼ã¹ãã¡ã¤ã«ã®æåã®æ¹ã«ä»¥ä¸ã®importæãå¿ è¦ã§ãã
import java.util.*;
â ããããçæããã
ãããã®çæã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
Map<ãã¼ã®ãã¼ã¿å, ãã¼ã¿ã®ãã¼ã¿å> ãããã®åå = new HashMap<ãã¼ã®ãã¼ã¿å, ãã¼ã¿ã®ãã¼ã¿å>();
ãããã®çæã§ã¯ããã¼ã¨ãã¼ã¿ã®ãã¼ã¿åã¨ã¨ãããã®ååãæå®ãã¾ãã
ä¾ãã°ããã¼ã¨ãã¼ã¿ãã¨ãã«Stringã®ãããmapãçæããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
Mapmap = new HashMap ();
â ãããã«ãã¼ã¿ãæ ¼ç´ããã
ãããã¸ã®ãã¼ã¿ã®æ ¼ç´ã¯ä»¥ä¸ã®ããã«è¡ãã¾ããã
ãããã®åå.put( ãã¼, ãã¼ã¿ );
ãããã¸ã®ãã¼ã¿ã®æ ¼ç´ã§ã¯ããããã®ååã¨ãã¼ã¨ãã¼ã¿ãæå®ãã¾ãããããã«æ ¼ç´ãããã¼ã¿ã¯ããã¼ã«é¢é£ä»ãããã¾ãã
ä¾ãã°ããããmapã«ãã¼ã¿ãæ ¼ç´ããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
map.put( "lemon", "é»" ); map.put( "greap", "ç´«" ); map.put( "pear", "ç·" );
â ããããããã¼ã¿ãåå¾ããã
ãããããã®ãã¼ã¿ã®åå¾ã¯ä»¥ä¸ã®ããã«è¡ãã¾ãã
ã¡ã½ãã( ãããã®åå.get( ãã¼ ) ); å¤æ° = ãããã®åå.get( ãã¼ ); return ãããã®åå.get( ãã¼ );
ãããããã®ãã¼ã¿ã®åå¾ã§ã¯ããããã®ååã¨ãã¼ãæå®ãã¾ãããã¼ã«é¢é£ä»ãããã¼ã¿ãããããããåå¾ããã¾ãã
ãããããåå¾ãããã¼ã¿ã¯ãå¤æ°ã«ä»£å
¥ããããã¡ã½ããã®å¼æ°ã«æ¸¡ããããã¡ã½ããã®æ»ãå¤ã¨ãã¦è¿ãããã§ãã¾ãã
ãªãããã¼ã«é¢é£ã¥ããã¼ã¿ãåå¨ããªãå ´åãnullãè¿ããã¾ãã
ä¾ãã°ããã¼ã¿ããããmapããåå¾ããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
System.out.println( map.get( "lemon" ) ); => é» System.out.println( map.get( "greap" ) ); => ç´« System.out.println( map.get( "cherry" ) ); => null
â ãããä¸ã®å ¨ã¦ã®ãã¼ã¿ã«åãå¦çãããã
ãããä¸ã®ãã¹ã¦ã®ãã¼ã¨ãã¼ã¿ã«åãå¦çãããã«ã¯ã以ä¸ã®ãããªforæã使ç¨ãã¾ãã
for ( ãã¼ã®ãã¼ã¿å key : ãããã®åå.keySet() ) { ãã¼ã¿ã®ãã¼ã¿å data = ãããã®åå.get( key ); // keyãdataã使ã£ãå¦ç; }
ãã®foræä¸ã®ããããã®åå.keySet()ãã¯ãããä¸ã®å
¨ã¦ã®ãã¼ãè¿ãã®ã§ãå
¨ã¦ã®ãã¼ã«ã¤ãã¦ä¸æ¬å¼§å
ã®å¦çãç¹°ãè¿ããã¾ããã¾ããä¸æ¬å¼§å
ã®å
é ã§ã¯ããã¼ã«é¢é£ã¥ãããã¼ã¿ãåå¾ãã¦ãã¾ãã
ãã®ããããã®foræã使ãã°ãããä¸ã®ãã¹ã¦ã®ãã¼ã¨ãã¼ã¿ã«åãå¦çããããã¨ãã§ãã¾ãã
ä¾ãã°ããã¼ã¨ãã¼ã¿ã®é¢é£ä»ãããã¹ã¦è¡¨ç¤ºããã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
for ( String key : map.keySet() ) { String data = map.get( key ); System.out.println( key + ": " + data ); }
ããã¯ã以ä¸ã®ããã«æ¸ããå ´åã¨åããããªçµæã«ãªãã¾ãããã ããmap.keySet()ãè¿ããã¼ã®é åºã¯æ±ºã¾ã£ã¦ããªãã®ã§ã表示é åºãç°ãªããã¨ãããã¾ãã
System.out.println( "apple: " + map.get( "apple" ) ); => apple: ç· System.out.println( "greap: " + map.get( "greap" ) ); => greap: ç´« System.out.println( "strawberry: " + map.get( "strawberry" ) ); => strawberry: 赤 System.out.println( "pear: " + map.get( "pear" ) ); => pear: ç· System.out.println( "lemon: " + map.get( "lemon" ) ); => lemon: é»
ãã¼ã¿æ§é
- Javaã使ããªãå¿ ãè¦ãã¦ãããããã¼ã¿æ§é - é åã»ãªã¹ãã»ããã
- PHPãªãè¦ããã¹ããã¼ã¿æ§é ã¯ã²ã¨ã¤ã ãï¼ - é å
- Perlã§è¦ããããã¼ã¿æ§é - é åã»ããã·ã¥
- VBAã§è¦ãã¦ãããã¼ã¿æ§é - éçé åã»åçé åã»ãã£ã¯ã·ã§ããª
- JavaScriptã§è¦ãã¦ããã¨ãããã¼ã¿æ§é - é åã»ãªãã¸ã§ã¯ã
- Bashã§è¦ãã¦ããã¨ãããã¼ã¿æ§é - é å - ä½ãããã®è¨èªã«ããè¨è¿°ã解æããæ¥è¨
ã¢ã«ã´ãªãºã
- Javaã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»ã½ã¼ãã»çµåã»éè¨ (ãªã¹ãï¼ãããç·¨)
- Javaã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»ã½ã¼ãã»çµåã»éè¨ (ãªã¹ãï¼ãã¼ã³ç·¨)
- PHPã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»ã½ã¼ãã»çµåã»éè¨
- VBAã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»çµåã»éè¨
- Javascriptã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»ã½ã¼ãã»çµåã»éè¨
- SQLã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã ï¼(ã¨ããããselectæã®æ¸ãæ¹) - whereã»order byã»joinã»group by
- Bashã使ããªãç解ãã¦ããããã¢ã«ã´ãªãºã - æ½åºã»ã½ã¼ãã»çµåã»éè¨