/******************************************************************/
/* Prof. Dr. Carsten Vogt                                         */
/* FH Koeln, Fak. 07 / Nachrichtentechnik                         */
/* http://www.nt.fh-koeln.de/vogt                                 */
/*                                                                */
/* Das Programm demonstriert grundlegende Operationen auf Arrays. */
/******************************************************************/

import java.io.*;

public class Array {

 public static void main(String[] args) throws IOException  {

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

  /* Deklaration von Arrayvariablen ohne oder mit Initialisierung */

  short[] a;
  int[] b = new int[3];
  double[] c = { 1.2, 2.3, 3.4, 4.5 };

  /* Initialisierung der Arrayvariable a mit einem Array,
     der eine benutzergewaehlte Laenge hat */ 

  System.out.println();
  System.out.print("Gewuenschte Laenge von Array a: ");
  int lg = Integer.parseInt(in.readLine());
  a = new short[lg];  

  /* Ausgabe der anfaenglichen Arrayinhalte
     unter Benutzung von for-Schleifen */

  System.out.println();
  System.out.println("Anfangswerte der Arrays:");
  System.out.println();
  System.out.print("a:");
  for (int i=0; i<a.length; i++) 
   System.out.print(" "+a[i]);
  System.out.println();

  System.out.println();
  System.out.print("b:");
  for (int i=0; i<b.length; i++) 
   System.out.print(" "+b[i]);
  System.out.println();

  System.out.println();
  System.out.print("c:");
  for (int i=0; i<c.length; i++) 
   System.out.print(" "+c[i]);
  System.out.println();

  /* Zuweisung neuer Werte an einen Array durch Benutzereingaben
     und anschliessende Ausgabe der Werte */
 
  System.out.println();
  System.out.println("Bitte Werte fuer a-Komponenten eingeben:");
  for (int i=0; i<a.length; i++) {
   System.out.print("a["+i+"]: ");
   a[i] = Short.parseShort(in.readLine());
  };  

  System.out.println();
  System.out.print("a nach Eingabe:");
  for (int i=0; i<a.length; i++)
   System.out.print(" "+a[i]);

  /* Erzeugung einer neuen Arrayvariablen d,
     die auf denselben Array wie c verweist */

  double[] d;
  d = c;
  System.out.println(); System.out.println();
  System.out.println("c und d verweisen auf denselben Array:");
  System.out.println();
  System.out.println(" vor Zuweisung an d[0]:   c[0]=" + c[0] + "  d[0]=" + d[0]);
  d[0] = 5.6;  /* aendert auch c[0]! */
  System.out.println();
  System.out.println(" nach Zuweisung an d[0]:  c[0]=" + c[0] + "  d[0]=" + d[0]);
  
  /* Erzeugung einer neuen Arrayvariablen e, die auf einen Array verweist,
     der seine Anfangswerte aus dem Array von c bezieht. e und c verweisen
     aber hier (im Unterschied zu oben) auf unterschiedliche Arrays. */

  double[] e;
  e = (double[]) c.clone();
  System.out.println();
  System.out.println("e verweist auf einen neuen Array mit Anfangswerten aus dem c-Array:");
  System.out.println();
  System.out.println(" vor Zuweisung an e[0]:   c[0]=" + c[0] + "  e[0]=" + e[0]);
  e[0] = 7.8;  /* aendert c[0] nicht! */
  System.out.println();
  System.out.println(" nach Zuweisung an e[0]:  c[0]=" + c[0] + "  e[0]=" + e[0]);

  /* Kopieren des Teilbereichs eines Arrays durch System.arraycopy() */

  char[] f = new char[10],
         g = new char[10];

  for (int i=0;i<10;i++) {
   f[i] = (char) ('A'+i);
   g[i] = '-'; }

  System.out.println();
  System.out.println();
  System.out.println("Arrays von f und g im Anfangszustand:");
  System.out.print(" f:");
  for (int i=0; i<10; i++)
   System.out.print(" "+f[i]);
  System.out.println();
  System.out.print(" g:");
  for (int i=0; i<10; i++)
   System.out.print(" "+g[i]);
  System.out.println();

  System.arraycopy(f,5,g,2,4);

  System.out.println();
  System.out.println("Arrays nach Anwendung von arraycopy(f,5,g,2,4):");
  System.out.print(" f:");
  for (int i=0; i<10; i++)
   System.out.print(" "+f[i]);
  System.out.println();
  System.out.print(" g:");
  for (int i=0; i<10; i++)
   System.out.print(" "+g[i]);
  System.out.println();

 }

}

