Passing array values to methods & Classes

Hi,
I have written the following two classes, I want to pass the value from one class - insersortest to the other class what kind of return statement do I do ? I am not sure what does this Comparable object do in this program ? I am trying the program (algorithm specfieid) from one of the Data Structure book.
import java.io.*;
import java.lang.*;
public class Insersortest
public static void main(String args[]) throws IOException
Insersort ghl = new Insersort();
          Comparable a[] = {1, 3, 5, 9, 1};          
               System.out.println("Detecting duplicates"+ ghl.insertA(a));               
-=-=-=-
class Insersort
public static int insertA( Comparable [ ] a )
for( int p = 1; p < a.length; p++ )
Comparable tmp = a[ p ];
int j = p;
for( ; j > 0 && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
Could somebody provide their view please.
PK

You return arrays just like any object:
public Object[] getArray()Comparable is an interface. Any object that implements Comparable (and can therefore be stored in a Comparable's reference) is guarenteed to have a compareTo method.
Does that help?

Similar Messages

  • Pass array on to another class

    Hi. Simple question here.
    I have two classes and in one I have an array. I want to pass it on to the other class so I can use the values and stuff there. How do I do that?

    You don't pass things to classes. You pass them to constructors or methods of a class or instance of one.
    If you do this, for example:
    System.out.println("Hello world");
    you are passing a String argument to the println method.
    You can similarly pass your array to a method of some class, if that method accepts an array as a parameter.
    P.S. You're welcome. Obviously you didn't get hit by a bus and couldn't reply with a simple "thank you", as you posted another topic a day or so later than this. But, you're welcome.

  • Passing Array to Another Method

    Hello, I created a program with an array in one of the methods. I have been trying to figure out how to correctly pass the array to another method in the same class. I know my problem is in my method delcaration statements. Could someone please show me what I am doing wrong? Please let me know if you have any questions. Thanks for your help.
    import javax.swing.*;
    import java.util.*;
    class Bank1 {
         public static void main(String[] args) {
              Bank1 bank = new Bank1();
              bank.menu();
         //Main Menu that initializes other methods
         public void menu( ) {
              Scanner scanner = new Scanner(System.in);
              System.out.println("Welcome to the bank.  Please choose from the following options:");
              System.out.println("O - Open new account");
              System.out.println("T - Perform transaction on an account");
              System.out.println("Q - Quit program");
              String initial = scanner.next();
              char uInitial = initial.toUpperCase().charAt(0);
              while (uInitial != 'O' && uInitial != 'T' && uInitial != 'Q') {
                   System.out.println("That was an invalid input. Please try again.");
                   System.out.println();
                   initial = scanner.next();
                   uInitial = initial.toUpperCase().charAt(0);
              if (uInitial == 'O') newAccount();
              if (uInitial == 'T') transaction();
         //Method that creates new bank account
         public Person[] newAccount( ) {
              Person[] userData = new Person[1];
              for (int i = 0; i < userData.length; i++) {
                   Scanner scanner1 = new Scanner(System.in);
                   System.out.println("Enter your first and last name:");
                   String name = scanner1.next();
                   Scanner scanner2 = new Scanner(System.in);
                   System.out.println("Enter your address:");
                   String address = scanner2.next();
                   Scanner scanner3 = new Scanner(System.in);
                   System.out.println("Enter your telephone number:");
                   int telephone = scanner3.nextInt();
                   Scanner scanner4 = new Scanner(System.in);
                   System.out.println("Enter an initial balance:");
                   int balance = scanner4.nextInt();
                   int account = i + 578;
                   userData[i] = new Person( );
                   userData.setName               ( name );
                   userData[i].setAddress          ( address );
                   userData[i].setTelephone     ( telephone );
                   userData[i].setBalance          ( balance     );
                   userData[i].setAccount          ( account     );
                   System.out.println();
                   System.out.println("Your bank account number is: " + userData[i].getAccount());
              return userData;
              menu();
         //Method that gives transaction options
         public void transaction(Person userData[] ) {
              System.out.println(userData[0].getBalance());

    Thank you jverd, I was able to get that to work for me.
    I have another basic question about arrarys now. In all of the arrary examples I have seen, the array is populated all at once using a for statement like in my program.
    userData = new Person[50];
    for (int i = 0; i < userData.length; i++) In my program though, I want it to only fill the first array parameter and then go up to the main menu. If the user chooses to add another account, the next spot in the array will be used. Can someone point me in the right direction for doing this?

  • Pass a value to a class that extends AbstractTableModel

    Hi
    I have a problem with a table model that I cannot find a way of overcoming.
    I have created a class called MyTableModel that extends AbstractTableModel.
    MyTableModel creates and uses another class that I have defined in order to retrieve records from a database, MyTableModel fills the table with these records.
    However, I wish to alter my class in order to provide an int parameter that will act as a �where� value in the classes sql query.
    The problem is this, I cannot work out how to pass a value into MyTableModel. I have tried creating a simple constructor in order to pass the value but it doesn�t work.
    My code is shown below:
    import BusinessObjects.JobItemClass;
    import DBCommunication.*;
    import java.util.ArrayList;
    import javax.swing.table.AbstractTableModel;
    public class MyJobItemTableModel extends AbstractTableModel
      public MyJobItemTableModel(int j)
          jobNo = j;
      int jobNo;
      JobAllItems jobItems = new JobAllItems(jobNo);
      ArrayList items = jobItems.getItems();
      String columnName[] = { "Item Number", "Item Description", "Cost per item", "Quantity" };
      int c = items.size();
      Class columnType[] = { Integer.class, String.class, Double.class, Integer.class };
      Object table[][] =  new Object[c][4];
      public void fillTable()
          if(c > 0)
            for (int i = 0; i < c; i = i + 1)
              this.setValueAt(((JobItemClass) items.get(i)).getItemNumber(), i, 0);
              this.setValueAt(((JobItemClass) items.get(i)).getDescription(), i, 1);
              this.setValueAt(((JobItemClass) items.get(i)).getCostPerItem(), i, 2);
              this.setValueAt(((JobItemClass) items.get(i)).getQuantity(), i, 3);
      public void setJobNo(int s)
          jobNo = s;
      public int getColumnCount()
          if(c > 0)
            return table[0].length;
          else
              return 0;
      public int getRowCount()
        return table.length;
      public Object getValueAt(int r, int c)
        return table[r][c];
      public String getColumnName(int column)
        return columnName[column];
      public Class getColumnClass(int c)
        return columnType[c];
      public boolean isCellEditable(int r, int c)
        return false;
      public void setValueAt(Object aValue, int r, int c)
          table[r][c] = aValue;
    }Any advice will be appreciated.
    Many Thanks
    GB

    your JobAllItems is created before constructor code is run (since it is initialized in class member declaration)
    use something like
    public MyJobItemTableModel(int j)
          jobNo = j;
          items = (new JobAllItems(jobNo)).getItems();
          c = items.size();
          table =   new Object[c][4];
      int jobNo;
      ArrayList items;
      String columnName[] = { "Item Number", "Item Description", "Cost per item", "Quantity" };
      int c;
      Class columnType[] = { Integer.class, String.class, Double.class, Integer.class };
      Object table[][] ;instead of
    public MyJobItemTableModel(int j)
          jobNo = j;
      int jobNo;
      JobAllItems jobItems = new JobAllItems(jobNo);
      ArrayList items = jobItems.getItems();
      String columnName[] = { "Item Number", "Item Description", "Cost per item", "Quantity" };
      int c = items.size();
      Class columnType[] = { Integer.class, String.class, Double.class, Integer.class };
      Object table[][] =  new Object[c][4];

  • Passing array values to an Applet from JSP

    Hi,
    I have a JSP page in which I've arrays that are populated from a DB. I want to pass these array values from the JSP page to a Applet in the same page. Is it possible ?
    Any help will be appreciated.
    :-) Praveen

    Then the answer is no. The Applet doesn't exist until it is instanciated by the browser so the JSP running on the server can not pass Java Objects. You can either set up a javascript array, set the parameter tags in the HTML Applet tag or have the Applet open a URLConnection back to the server.

  • Help in passing array values

    Hi,
    I have a procedure which accepts in parameter
    email_list. this is of type merge_email.
    Now merge_email is a record type:
    TYPE merge_email_value IS RECORD (
    contact_type_id number,
    contact_email_id varchar2,
    email_address varchar2,
    email_priority varchar2
    type merge_email is table of merge_email_value ;
    i want to test this procedure. how will i pass multiple values to this parameter?
    eg:
    1'st set of email:
    1,2,[email protected],3
    2'st set of email:
    1,2,[email protected],2
    i want to pass these two sets of values at a time to this IN parameter email_list. How can i achieve this?
    Thanks for your help
    Edited by: user13294597 on Jun 17, 2010 4:53 AM

    Hi blueforg,
    It is not an SQL type...but a PL type... and karthik had put another proc in the pkg to assign the values...but i suppose it is not possible to assign values from outside the pkg....smthng like you have done...
    i tried karthiks approach by defining a similar type outside
    eg:
    declare
        type t1_temp is record(contact_type_id number,
    contact_email_id varchar2(20),
    email_address varchar2(20),
    email_priority varchar2(10));--same record as defined in PKG.
        t1_temp1 t1_temp:=t1_temp();
    begin
    t1_temp1.extend();
    t1_temp1(1).id1:=1;
    t1_temp1(1).id2:=1;
    t1_temp1.extend();
    t1_temp1(1).id1:=2;
    t1_temp1(1).id2:=2;
    p_test_pkg.p_test(t1_temp1);
    end;--this does not seem to work...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How can i pass the values to method public static void showBoard(boolean[][

    I need x and y to pass to the method
    public static void showBoard(boolean[][] board
    i am very confused as to why its boolean,i know its an array but does that mean values ar true or false only?Thanks
    import java.util.Random;
    import java.util.Scanner;
    public class Life1
         public static void main(String[] args)
              int x=0;
              int y=0;
              Scanner keyIn = new Scanner(System.in);
              System.out.println("Enter the first dimension of the board : ");
              x = keyIn.nextInt();
              System.out.println("Enter the second dimension of the board : );
              y = keyIn.nextInt();
              boolean[][] board = new boolean[x][y];
              fillBoard(board);
              showBoard(board);
              //Ask the user how many generations to show.
              board = newBoard(board);
              showBoard(board);
         //This method randomly populates rows 5-9 of the board
         //Rewrite this method to allow the user to populate the board by entering the
         //coordinates of the live cells.  If the user requests that cell 1, 1 be alive,
         //your program should make cell 0,0 alive.
         public static void fillBoard(boolean[][] board)
              int row, col, isAlive;
              Random picker = new Random();
              for(row = 4; row < 9; row++)
                   for(col = 4; col < 9; col++)
                        if (picker.nextInt(2) == 0)
                          board[row][col] = false;
                        else
                          board[row][col] = true;
         //This method displays the board
         public static void showBoard(boolean[][] board)
              int row, col;
              System.out.println();
              for(row=0; row < x; row++)
                   for(col=0; col<y; col++)
                        if (board[row][col])
                             System.out.print("X");
                        else
                             System.out.print(".");
                   System.out.println();
              System.out.println();
         //This method creates the next generation and returns the new population
         public static boolean[][] newBoard(boolean[][] board)
              int row;
              int col;
              int neighbors;
              boolean[][] newBoard = new boolean[board.length][board[0].length];
              makeDead(newBoard);
              for(row = 1; row < board.length-1; row++)
                   for(col = 1; col < board[row].length-1; col++)
                        neighbors = countNeighbors(row, col, board);
                        //make this work with one less if
                        if (neighbors < 2)
                             newBoard[row][col]=false;
                        else if (neighbors > 3)
                             newBoard[row][col] = false;
                        else if (neighbors == 2)
                             newBoard[row][col]= board[row][col];
                        else
                             newBoard[row][col] = true;
              return newBoard;
         //This method counts the number of neighbors surrounding a cell.
         //It is given the current cell coordinates and the board
         public static int countNeighbors(int thisRow, int thisCol, boolean[][] board)
              int count = 0;
              int row, col;
              for (row = thisRow - 1; row < thisRow + 2; row++)
                   for(col = thisCol - 1; col < thisCol + 2; col++)
                     if (board[row][col])
                          count++;
              if (board[thisRow][thisCol])
                   count--;
              return count;
         //This method makes each cell in a board "dead."
         public static void makeDead(boolean[][] board)
              int row, col;
              for(row = 0; row < board.length; row++)
                   for(col = 0; col < board[row].length; col++)
                        board[row][col] = false;
    }

    this is what im workin with mabey you can point me in the right directionimport java.util.Random;
    /* This class creates an application to simulate John Conway's Life game.
    * Output is sent to the System.out object.
    * The rules for the Life game are as follows...
    * Your final version of the program should explain the game and its use
    * to the user.
    public class Life
         public static void main(String[] args)
              //Allow the user to specify the board size
              boolean[][] board = new boolean[10][10];
              fillBoard(board);
              showBoard(board);
              //Ask the user how many generations to show.
              board = newBoard(board);
              showBoard(board);
         //This method randomly populates rows 5-9 of the board
         //Rewrite this method to allow the user to populate the board by entering the
         //coordinates of the live cells.  If the user requests that cell 1, 1 be alive,
         //your program should make cell 0,0 alive.
         public static void fillBoard(boolean[][] board)
              int row, col, isAlive;
              Random picker = new Random();
              for(row = 4; row < 9; row++)
                   for(col = 4; col < 9; col++)
                        if (picker.nextInt(2) == 0)
                          board[row][col] = false;
                        else
                          board[row][col] = true;
         //This method displays the board
         public static void showBoard(boolean[][] board)
              int row, col;
              System.out.println();
              for(row=0; row < 10; row++)
                   for(col=0; col<10; col++)
                        if (board[row][col])
                             System.out.print("X");
                        else
                             System.out.print(".");
                   System.out.println();
              System.out.println();
         //This method creates the next generation and returns the new population
         public static boolean[][] newBoard(boolean[][] board)
              int row;
              int col;
              int neighbors;
              boolean[][] newBoard = new boolean[board.length][board[0].length];
              makeDead(newBoard);
              for(row = 1; row < board.length-1; row++)
                   for(col = 1; col < board[row].length-1; col++)
                        neighbors = countNeighbors(row, col, board);
                        //make this work with one less if
                        if (neighbors < 2)
                             newBoard[row][col]=false;
                        else if (neighbors > 3)
                             newBoard[row][col] = false;
                        else if (neighbors == 2)
                             newBoard[row][col]= board[row][col];
                        else
                             newBoard[row][col] = true;
              return newBoard;
         //This method counts the number of neighbors surrounding a cell.
         //It is given the current cell coordinates and the board
         public static int countNeighbors(int thisRow, int thisCol, boolean[][] board)
              int count = 0;
              int row, col;
              for (row = thisRow - 1; row < thisRow + 2; row++)
                   for(col = thisCol - 1; col < thisCol + 2; col++)
                     if (board[row][col])
                          count++;
              if (board[thisRow][thisCol])
                   count--;
              return count;
         //This method makes each cell in a board "dead."
         public static void makeDead(boolean[][] board)
              int row, col;
              for(row = 0; row < board.length; row++)
                   for(col = 0; col < board[row].length; col++)
                        board[row][col] = false;
    }

  • How to get array values from one class to another

    Supposing i have:
    - a class for the main()
    - another for the superclass
    And i want to create a subclass that does some function on an array object of the the superclass, how can i do so from this new subclass while keeping the original element values?
    Note: The values in the array are randomly generated integers, and it is this which is causing my mind in failing to comprehend a solution.
    Any ideas?

    If the array is declared as protected or public in the superclass you can directly access it using its identifier. If not, maybe the superclass can expose the array via some getter method.
    If this functionality can be generified (or is generic enough) to apply to all subclasses of the superclass the method should be moved to the superclass to avoid having to reproduce it in all the subclasses.

  • Passing array to a method through actionevent? ??

    what the hell is this guy on about your asking..
    well.. i have a user defined data structures (people)
    and i'm creating my first gui, so i have an action caught with
    public static void main(String[] args)
    int MAX = 1000;
    person[] ppl;
    ppl = new person[MAX];
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == addNewPerson) {
    //method call in here to add new person
    //but how can i pass the array this far so i can then give it
    //to my method, or am i looking at this all wrong?
    }if as the comments suggests, i'm calling this all wrong, what way should i implement this? i'm basically getting cannot resolve symbol, pointing to my method which adds a new person (so as i said before, it needs to be passed to the method)

    You might want start by reading a few books on OO design first.
    What you will probably want to do is have a class which stores the array and extends ActionListener...
    public class Gui extends ActionListener {
      Person[] ppl = new Person[100];
      Component comp;
      Button addPersonButton;
      public Gui () {
        comp = new .....;
        addPersonButton = new Button("Add person");
        comp.add(addPersonButton);
        comp.addActionListener(this);
      public static void main(String args[]) {
        Gui gui = new Gui();
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addPersonButton) {
          doAddPerson();
      pulbic void doAddPerson() {
        ppl[0] = ....
    }Hope this shows you a bit more. Not sure how good my design of gui's is though :-/
    Rob.

  • Passing Array to a method

    Hi,
    There is a class MyClass that implements Serializable. I need to create array of this class and pass to a method.
    In java, this can be done as following:
                   MyClass[] myClassVariables=new MyClass[2];
                   MyClass myClassInstance1=new MyClass();
                   MyClass myClassInstance2=new MyClass();
                   myClassVariables[0]=myClassInstance1;
                   myClassVariables[1]=myClassInstance2;
                   String message = getValues(myClassVariables);
    import java.io.Serializable;
    public class MyClass implements Serializable{
    public String getValues(MyClass[] myClassVariables)
    When I implement this in a workflow, I get the following error:
    XPRESS exception:
    com.waveset.util.WavesetException: Couldn't find method getValues(java.lang.String, java.lang.String) in class MyClass
    java.lang.NoSuchMethodException: MyClass.getValues(java.lang.String, java.util.ArrayList)
    Code I am using is as following:
    <set name='myClassInstance1'>
    <new class='com.marathon.MyClass'/>
    </set>
    <set name='myClassInstance2'>
    <new class='com.marathon.MyClass'/>
    </set>
    <set name='myList'>
    <new class='java.util.ArrayList'/>
    </set>
    <set name='myClassVariables'>
    <list>
    <ref>myClassInstance1</ref>
    <ref>myClassInstance2</ref>
    </list>
    </set>
    <set name='message'>
    <invoke name='getValues' class='MyClass'>
    <ref>myClassVariables</ref>
    </invoke>
    </set>
    <ref>message</ref>
    Please help me,
    Thanks

    You might want start by reading a few books on OO design first.
    What you will probably want to do is have a class which stores the array and extends ActionListener...
    public class Gui extends ActionListener {
      Person[] ppl = new Person[100];
      Component comp;
      Button addPersonButton;
      public Gui () {
        comp = new .....;
        addPersonButton = new Button("Add person");
        comp.add(addPersonButton);
        comp.addActionListener(this);
      public static void main(String args[]) {
        Gui gui = new Gui();
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addPersonButton) {
          doAddPerson();
      pulbic void doAddPerson() {
        ppl[0] = ....
    }Hope this shows you a bit more. Not sure how good my design of gui's is though :-/
    Rob.

  • How to pass one value from method 1 to method 2 in BADI...

    Hello Experts,
    How do I pass a custom variable from lets say method 1 to method 2 in BADI? Do I need to
    enhance it? For example, I need to pass my flag variable which contains 'X' from method as exporting
    and importing in method 2.

    Hi,
    Yes declare a flag in first method and make it 'X' when you get the condition that you have made changes to item values and export it to memory id ...
    EXPORT GV_FLAG TO MEMORY ID 'SSS'.
    and in second method import the same...
    IMPORT GV_FLAG FROM MEMORY ID 'SSS'.
    and according to the value write the logic...
    Thanks,
    Shailaja Ainala.

  • URGENT: Passing Array from JSP to a Stored Procedure

    Hi,
    Can some one please help me understanding how can I pass array from JSP page to a stored procedure in database.
    Thanks in advance.
    Jatinder

    Thanks.
    I tried ArrayExampla.java and was successful in passing array values to the stored database procedure.
    How can I use this class in JSP? Like I have first JSP where in I will collect input from the user and then submit it to the second JSP - that needs to call the ArrayExample.java to pass the values as array to the database.
    How should I call this java code in my second JSP?
    Thanks in advance.

  • Passing values from method array to class array

    HELP!! Plz excuse any ignorance. Am a noob and have created an array within my class. I then try to alter those array values from inside one of the classes methods. When I try to access the new values from the class array, they don't exist. I think it's a duration/scope issue and am struggling to get around it. This is the only way I can implement the task required and would appreciate any advice you can thorw. cheers in advance.. =~D

    I suspect that you're altering an array passed as a parameter, rather than array that's a field of the instance, but as you didn't post any of your code, that can only be a guess.

  • Passing values between methods in the same class

    Hi,
    How to pass internal tables or values between methods in the same class.
    How to check if the internal method in another method is initial or not.
    How to see if the method has already been executed.
    Thanks.

    Just declare the internal table as an attribute in the class - that way every method in this class has access to it.
    Since any method has access to all class attributes you can easily check if the internal table is initial or not.
    I am not aware of any standard functionality if a method has already been executed or not, one way would be to declare a class attribute for each method and set it once the method has been executed. That way every method in that class would know which method has already been executed or not.
    Hope that helps,
    Michael

  • Change variable values in a class without acessing it's methods?

    I have the following scenery, a main class with:
    -ArrayList of strings.
    -Class frame (with textfield and button) supposed to add strings to that Array.
    Ok, in main class i set the frame to run, but how to make the frame to change the ArrayList of the main class in a easy way? What is the easiest solution and the best without hurting the paradigm?
    1-One solution would be to make a method in frame that returns the string and IN MAIN CLASS add it. But is this too straightforward? Like, if i had to manage 7,8 frames that must acess 7, 8 ArrayLists of main class as well as other frames recursively (Example: main class start running frame 1, then if button1 pressed go to frame 2, if button 2 pressed get textfield and chang ArrayList)
    2-Another solution would be to set all ArrayLists as static variables so i can change them freely? Is this a good paradigm?
    3-Pass the own main class as parameter throughout the frames? Is it possible? If so please post a 10, 15 line code in which i could do that.
    4- Put the main class as mother and all frames as son and put all arraylists as protected?
    I feel like im lacking one good paradigm, it should exist some concept in which the created object could change the value of the variable passed as parameter in the class that created it.
    Thank you very much.

    Thank you for your reply Kayaman, but i think you should help me.
    I tried to pass a simple variable (a string) as parameter through the constructor to one frame, like any other class.
    public class TelaLogin extends javax.swing.JFrame {
    String a;
    public TelaLogin(String a) {
    this.a=a;
    initComponents();
    But it returns a compilation error, maybe is something in it's main method? I didn't change it:
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new TelaLogin().setVisible(true);
    The main class has a 'new TelaLogin' that looks very suspicious. Is it that?
    This looks very simple.
    What must i change here? Please Kayaman help me.

Maybe you are looking for

  • Can I use a Sony all-in-one PC/TV as a second display for the G5?

    I'm trying to build a pro music system out of my G5, which means that it should only have music software on it. I have an aluminum 20" display hooked up to my Radeon x800T video card, which has one DVI and one ADC jack on it. A pro music rig needs tw

  • Question for a class

    Hey, I'm doing basic flash in a school class right now, and I have a question... ... there's a "vegetable term" used to describe following/seeing the motion path an object is taking. Do any of you know what it is?

  • Admin user removed from Permissions

    After upgrading my wiki server to 10.6.4 I found the admin user was no longer part of the Administrative permissions for each users wiki. I check their metadata.plist file and sure enough the admin user ( and other users ) was missing from the admin

  • Upgrading iOS 5.1.1 to iOS 7

    Hi, I have a iPad and wanted to upgrade to iOS 7. I was forced to use iTunes to upgrade to iOS 5.1.1 from 4.0, but now I am not able to upgrade to iOS7 becuse it says 'Your iOS version is up to date"

  • Negative Images

    I set a black background for safari, and images in the browser are showing up negative. How can I set general backgrounds back to white? I have been through system preferences and safari preferences and can't figure out how to fix my mistake mini   M