2 d array in ein anderes 2 d array kopieren. !

Status
Nicht offen für weitere Antworten.

besi

Mitglied
Hi, ich würde mich freun, wenn ihr mir helfen könntet... denn die board suche hat in meinem fall nichts ergeben das mir hilft...

ich möchte ein 2 d array in ein anderes 2 d array kopieren und habe dazu folgenden code geschrieben... der aber nicht funktioniert... warum nicht und wie kann ichs besser machen??

Code:
 for (int i=0; i<15; i++)
         {  		
    		
        	for (int j=0; j<15; j++)
            {
        		feld2[i][j]=feld[i][j];
            }
        }

das array is 15*15 groß


bitte helft mir... bidde bidde bidde
danke!!

mfg besi
 
das bekome ich:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at World.umschreiben(World.java:44)
at Main.keyTyped(Main.java:91)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


gibs das system array copy auch für 2d ??

mfg besi
 
Code:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;


public class World
 {
    final int H=15;
    final int W=15;
    final int B=40;
    Frame f;
    int xPos;
    int yPos;
    Image worldImg, aspImg;
    public int [][]feld2;

    public int [][]feld={
          {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},        //Array mit 4 Werten   4=Keks zum Fressen der Geister
          {3,4,2,2,2,2,3,3,3,2,2,2,2,4,3},        //                     3=Wand
          {3,2,3,3,3,2,2,2,2,2,3,3,3,2,3},        //                     2=freier Weg mit Keks
          {3,2,3,2,2,2,3,3,3,2,2,2,3,2,3},        //                     1=freier Weg
          {3,2,3,2,3,2,2,2,2,2,3,2,3,2,3},
          {3,2,2,2,3,2,3,2,3,2,3,2,2,2,3},
          {3,3,2,3,3,2,3,2,3,2,3,3,2,3,3},
          {3,3,2,3,2,2,3,2,3,2,2,3,2,3,3},
          {3,3,2,3,3,2,3,2,3,2,3,3,2,3,3},
          {3,2,2,2,3,2,3,2,3,2,3,2,2,2,3},
          {3,2,3,2,3,2,2,2,2,2,3,2,3,2,3},
          {3,2,3,2,2,2,3,3,3,2,2,2,3,2,3},
          {3,2,3,3,3,2,2,2,2,2,3,3,3,2,3},
          {3,4,2,2,2,2,3,3,3,2,2,2,2,4,3},
          {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}
        };
    
    
    public void umschreiben ()
    {
    	for (int i=0; i<15; i++)
        {  		
    		
        	for (int j=0; j<15; j++)
            {
        		feld2[i][j]=feld[i][j];
            }
        }  
    }
    
   
   
   public World (int xPos,int yPos, Frame f,Image worldImg,Image aspImg)  // Konstruktor: übergabe von  
   {																      // Bildern, Position und Frame
     this.xPos=xPos;
     this.yPos=yPos;
     this.f=f;
     this.worldImg=worldImg;
     this.aspImg=aspImg; 
   }
   
   public void drawWorld(Graphics g)
    {
    //System.out.println(feld2[1][1]); 	
      g.drawImage(worldImg,xPos,yPos,f);
      for (int x = 0; x<W; x++)
        {
            for(int y = 0; y<H; y++ )
            {
               g.setColor(Color.yellow);
               
               if(feld[x][y]==2)									//Punkte
               {
                  g.fillOval(xPos+x*B+20,yPos+y*B+20,7,7);
               }

               g.setColor(Color.blue);
               
               if(feld[x][y]==4)									//Aspirin tabletten
               {
               	 g.drawImage(aspImg,xPos+x*B+3,yPos+y*B,f);	
                  
               	//g.fillOval(xPos+x*B+20,yPos+y*B+20,10,10);
               }
               
            }

        }  
        //g.drawImage(img,xPos,yPos,f);
      }
}
 
hmmm... ok... ich dachte das reicht... muss ich da nochmal die ganzen werte reinschreiben... ?? nee oda...

hmm danke aba... bin irgendwie zu doof

mfg besi
 
besi hat gesagt.:
hmmm... ok... ich dachte das reicht... muss ich da nochmal die ganzen werte reinschreiben... ?? nee oda...
Dann hättest du's ja schon kopiert :wink:
Code:
public int [][]feld2 = new int[feld1.length] [feld1[0].length]
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben