jak odczytać tablice wielowymiarową w JavaME?

0

Witam
chce zrobić program, który w tablicy ma pewne liczby oraz druga tablica litery
chce dodać do elementu tablicy np: 5

problem taki jak dodać tablice wielowymiarową do jave me i z niej odczytać

do mojego programu dodałem tablicę ale jej nie wyświetla

podaje kod

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author komp_k
 */
public class HelloMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;

    private Command exitCommand;
    private Command backCommand;
    private Form form;
    private StringItem stringItem;
    private List list;
    private Form form1;

	int[][] tablica = new int[4][7];
//tablica[0] = new int[3];
//tablica[1] = new int[2];
//tablica[2] = new int[1];
	
	
	
	
	
	
	
    public HelloMIDlet() {
    }
    private void initialize() {
    }
    public void startMIDlet() {
        switchDisplayable(null, getList());
    }
    public void resumeMIDlet() {
    }
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    }
    public void commandAction(Command command, Displayable displayable) {
        if (displayable == form) {
            if (command == backCommand) {
                switchDisplayable(null, getList());
            }
        } else if (displayable == form1) {
            if (command == backCommand) {
                switchDisplayable(null, getList());
            }
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) {
                listAction();
            } else if (command == exitCommand) {
                exitMIDlet();
            }
        }
    }
    public Command getExitCommand() {
        if (exitCommand == null) {
            exitCommand = new Command("Exit", Command.EXIT, 0);
        }
        return exitCommand;
    }
    public Form getForm() {
        if (form == null) {
            form = new Form("Welcome", new Item[]{getStringItem()});
            form.addCommand(getBackCommand());
            form.setCommandListener(this);
        }
        return form;
    }
    public StringItem getStringItem() {
        if (stringItem == null) {
            stringItem = new StringItem("Hello", "Hello, World!");
        }
        return stringItem;
    }
	
    public Command getBackCommand() {
        if (backCommand == null) {
            backCommand = new Command("Back", Command.BACK, 0);
        }
        return backCommand;
    }
    public List getList() {
        if (list == null) {
            list = new List("list", Choice.IMPLICIT);
            list.append("List Element 1", null);
            list.append("List Element 2", null);
            list.addCommand(getExitCommand());
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[]{false, false});
        }
        return list;
    }
    public void listAction() {
        String __selectedString = getList().getString(getList().getSelectedIndex());
        if (__selectedString != null) {
            if (__selectedString.equals("List Element 1")) {
                switchDisplayable(null, getForm());
            } else if (__selectedString.equals("List Element 2")) {
                switchDisplayable(null, getForm1());
            }
        }
    }
    public Form getForm1() {
        if (form1 == null) {
            form1 = new Form("form1");
		int i;	
		int j;
                for(i=0; i< tablica.length; i++)
		  for(j=0; j< tablica[i].length; j++)
			  tablica[i][j] = 0;
			  form1.append(tablica[i][j]);
			  
            form1.addCommand(getBackCommand());
            form1.setCommandListener(this);
          
        }
        return form1;
    }

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}

jeszcze nie miałem styczności z tablicami javame
dziękuje za odpowiedz

0

Nie znam Java ME, ale sposób, który zakomentowałeś jest niepoprawny również w Java SE. Deklaracja tworzy tablicę prostokątną, a potem próbujesz tworzyć wiersze różnej
długości. Jeżeli tablica ma być prostokątna, to

int[][] tab = new int[4][7]; //tworzenie tablicy
tab[2][3] = 55; //wpisywanie wartości do tablicy

Jeżeli tablica nie ma być prostokątna, to tworzenie wygląda trochę inaczej, np. tak:

int[][] tab = new int[4][];
for(int i=0;i<tab.length;i++)
{
    tab[i] = new int[i+3]
}
0

niestety taki kod jest błędny w JavaME

0

witam
udało mi się zrobić samemu tablice jedno i dwu wymiarową, ale mam problem
nie pamiętam jak dodać lub pomnożyć całą tablicę przez np: 4

  1. po kompilacji odczytuje mi dwa wiersze z tablicy jednej jak i drugiej tablicy, kolejne wiersze pomija nie wiem dlaczego.
  2. gdy chce w string dodać kolejną wiersz wyskakuje mi błąd
  3. jak zmniejszyć ilość spacje od jednej kolumny do drugiej jak i w pionie i poziomie
    np:
    4| 24Rm 53R 5
    5| 34Rm 52R 6
    a nie jak
    4| 24Rm 53R 5

5| 34Rm 52R 6

3.co zamiast for (int i=0;i< 5;i++)
co zamiast piątki mam wpisać , chodzi mi czy muszę za każdym razem zliczać tablicę, a tego nie chce

prosiłbym o pomoc
podaje kod

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author komp_k
 */
public class HelloMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;

    private Command exitCommand;
    private Command backCommand;
    private Form form;
    private StringItem stringItem;
    private List list;
    private Form form1;
   // private final String string[] = {"5","2","3","4","5","6","7","8" };
	private final String string[][] = {{"10Rm","15", "40Rk", "55"},
        {"20Rm","135", "44Rk", "565"}};
	
	
    public HelloMIDlet() {
    }
    private void initialize() {
    }
    public void startMIDlet() {
        switchDisplayable(null, getList());
    }
    public void resumeMIDlet() {
    }
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    }
    public void commandAction(Command command, Displayable displayable) {
        if (displayable == form) {
            if (command == backCommand) {
                switchDisplayable(null, getList());
            }
        } else if (displayable == form1) {
            if (command == backCommand) {
                switchDisplayable(null, getList());
            }
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) {
                listAction();
            } else if (command == exitCommand) {
                exitMIDlet();
            }
        }
    }
    public Command getExitCommand() {
        if (exitCommand == null) {
            exitCommand = new Command("Exit", Command.EXIT, 0);
        }
        return exitCommand;
    }
    public Form getForm() {
        if (form == null) {
            form = new Form("Welcome", new Item[]{getStringItem()});
            
            for(int i=0; i<5; i++)
             
            {  
                for(int j=0; j<5; j++)
            {
                string[i][j]+=3;
            form.append(string[i][j]);
            }
            }
            form.addCommand(getBackCommand());
            form.setCommandListener(this);
        }
        return form;
    }
    public StringItem getStringItem() {
        if (stringItem == null) {
            stringItem = new StringItem("Hello", "Hello, World!");
        }
        return stringItem;
    }
	
    public Command getBackCommand() {
        if (backCommand == null) {
            backCommand = new Command("Back", Command.BACK, 0);
        }
        return backCommand;
    }
    public List getList() {
        if (list == null) {
            list = new List("list", Choice.IMPLICIT);
            list.append("List Element 1", null);
            list.append("List Element 2", null);
            list.addCommand(getExitCommand());
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[]{false, false});
        }
        return list;
    }
    public void listAction() {
        String __selectedString = getList().getString(getList().getSelectedIndex());
        if (__selectedString != null) {
            if (__selectedString.equals("List Element 1")) {
                switchDisplayable(null, getForm());
            } else if (__selectedString.equals("List Element 2")) {
                switchDisplayable(null, getForm1());
            }
        }
    }
    public Form getForm1() {
        if (form1 == null) {
            form1 = new Form("form1");
		 
			 
            form1.addCommand(getBackCommand());
            form1.setCommandListener(this);
          
        }
        return form1;
    }

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1