Data/Container

배열 - 자바

jjryu 2012. 6. 19. 09:13

배열도 객체이긴 하지만 메소드는 없는 특수한 객체다.


.length

.clone()


public class G02 {

  public static void main(String[] args) {

    int[] months = {31, 28, ... };    // a special kind of initialization expression that must occur at the point where the array is created

/*

    int[] months;

    months = new int[] {31, 28, ... };

*/

/* or

    int[] months = new int[12];
    score[0]=31;
    score[1]=28;
    ...

*/

    System.out.println(Arrays.toString(months));    // JDK 5

/* c.f.

    System.out.println(Arrays.asList(months));

*/


    for(int month : months) {    // Java SE5; the foreach syntax

        ... month ...

    }

    /* or

    for(int i=0; i < months.length; i++) {

      ... months[i] ...

    }

    */

  }

}

[31, 28, ... ]


String[] fruitNames = { "Apple", "banana", ... };
/* or
String[] fruitNames = new String[] { "Apple", "banana", ... };
*/

String[] fruitNames = new String[5];

/* or

String[] fruitNames;

fruitNames = new String[5];

*/

fruitNames[0] = "Apple";

fruitNames[1] = "banana";

...


Point[] points = { new Point(1,2), new Point(3,4), ... };

/* or

Point[] points = new Point[] { new Point(1,2), new Point(3,4), ... };

*/

/* or

Point[] points;

points = new Point[] { new Point(1,2), new Point(3,4), ... };

*/


Color [] color = {Color.red, Color.blue, ... };


int [][]score=new int [5][3]; 

/*

or

int [][]score=new int [5][];

for(int i=0; i < 5; i++)

score[i] = new int[3];

*/


float[][][] globalTemperatureData = new float[360][180][100];


int [][]score = { { 85,  60,  ..},

                  { 90,  95,  ..},

                  ..

               };


new String[][] { { "Yes", "No" }, {"Out", "Non" }}


java.lang.System
.arraycopy() // static
/*
@SuppressWarnings("unchecked")
private final <T> T[] copy(T[] source) {
   Class type = source.getClass().getComponentType();
   T[] target = (T[])Array.newInstance(type, source.length);
   System.arraycopy(source, 0, target, 0, source.length);
   return target;
}
*/

java.util.Arrays
.toString() // static
.asList() // static
.sort() // static
.parallelSort() // static
.binarySearch() // static
.equals() // static
.copyOf() // static
c.f. .clone()
/*
    public Object (ArrayList<E>::)clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
*/