From int[][] to int[]?

I have a double array: int[][] of a variable length.
Now I would like to store each row of this double array in a single array, is that possible considering that it needs to work on a variable array length?
I have tried:
int[][] test = new int[s][s];
int[] row = new int[s];
for (int i = 0; i < s; i++){
     for (int j = 0; j < s; j++){
           row[j] = test[i][j];
     }But I need to create a new array each time the second for loop terminates..which I can't figure out to do.

As "row" is declared, you will overwrite the value every time the second loop terminates. I really don't understand what are you trying to do, maybe store "test" in a simple array?
Then try this:
int[][] test = new int[s][s];
int[] row = new int[s*s];
for (int i = 0; i < s; i++){
for (int j = 0; j < s; j++){
row[j+i*s] = test[i][j];
Was it?

Similar Messages

  • Can not cast from object to int

    Hi All,
    I have the following bunch of code
    Session session;
              session = getSessionFactory().openSession();
              Query query;
              int MaxPageId = 0;
              List list;
              try{
                   query = session.createQuery("select max(emp.id) from EmpBean emp");
                   list = query.list();
                   MaxPageId = (int) list.get(0);
                   return MaxPageId;
              catch(Exception e){
                   System.out.println(e.getMessage());
              return MaxPageId;
    The query executed successfully but when i get the max id in a integer variable by the following statement
    MaxPageId = (int) list.get(0);
    I get the following error "can not cast from object to int"
    Please suggest for the same.
    -Thanks

    Cast to an Integer, not an int, and let autoboxing turn the Integer into an int.
    int maxPageId;
    maxPageId = (Integer) list.get(0);You can't cast objects into primitives.
    Autoboxing isn't casting.
    And use Java naming conventions. Local variables start with a lower-case letter.

  • HT201269 I have years worth of music on my PC that is about to die.  If I get an iPad is there anyway to get the music from my PC onto the iPad? I have the iCloud but my understanding is only music actually purchased from iTunes lives int he cloud.

    I have years worth of music on my PC that is about to die.  If I get an iPad is there anyway to get the music from my PC onto the iPad? I have the iCloud but my understanding is only music actually purchased from iTunes lives int he cloud. I have music that was legally purchased and loaded to my iTunes account from cd's that I no longer own.  I need a new laptop and am deciding between the iPad and the Mac book.  I use the computer for music, photos, and web - nothing else and nothing fancy.  I don't know that there is a way to save my huge music library without saving it to an external hard drive and then loading it to a new one (does the iPad have a port to plug an external hard drive into?)

    First, you would need to make a backup of the entire iTunes folder on the PC. You could get music to the iPad if the PC was still functioning, or you would have to move the music to another computer. An iPad is not a laptop, and does not have a port to plug an external drive into. You can sync music to the iPad from the computer, but you are correct, unless you get iTunes Match, you would need a computer to copy the music to and then sync to an iPad.

  • Convert from char to int

    hey
    I've an input string ("1234")
    What i need to do is to fill a matrix 2*2 with the input string.
    I thought about running over the string, and by str.charAt(ind) to get a needed charters, but the question is ho do i convert charAt result to int?
    tanx

    igalep132 wrote:
    hey
    I've an input string ("1234")
    What i need to do is to fill a matrix 2*2 with the input string.
    I thought about running over the string, and by str.charAt(ind) to get a needed charters, but the question is ho do i convert charAt result to int?
    tanxThat's kinda vague. Do you mean convert '1' into the integer value 1 or convert '1' into the ASCII integer value of 49?
    Here's how to do the first:
    String one = "8";
    String two = "3";
    String numbers = "0123456789";
    int oneInt = numbers.indexOf(one);
    int twoInt = numbers.indexOf(two);
    System.out.println("oneInt = " + oneInt);
    System.out.println("twoInt = " + twoInt);If you meant the latter then ...
    char c = 'c';
    int cInt = (int)c;

  • My Firefox crashes every time I open it and none of the steps in the database solve the problem. My crash signature is PaintFrame(nsIRenderingContext*, nsIFrame*, nsRegion const&, unsigned int, unsigned int)

    I tried opening it in safe mode and none of the other help tips in the database on firefox crashes when I open it apply to me. I also ran a couple of virus programs to check for any infections and found nothing. I had been having issues with java previous to firefox beginning to crash. So I attempted to uninstall java from my computer but it did not help.
    My crash signature is PaintFrame(nsIRenderingContext*, nsIFrame*, nsRegion const&, unsigned int, unsigned int) and a search of this term on Mozilla Support pulled up no results.
    I'm not terribly knowledgeable with programming so any suggestions would help!

    I read online about a "clean reinstall" of firefox which seems to have worked! They should include direction on this on their page: https://support.mozilla.com/en-US/kb/Firefox+crashes+when+you+open+it

  • Memory leak in String(byte[] bytes, int offset, int length)

    Has anyone run into memory leak problem using this String(byte[] bytes, int offset, int length) class? I am using it to convert byte array to string, and I am showing memory leak using this class. Any idea what is going on?

    Hi,
    If you post in Native methods forum I assume you are using this constructor in the native side.
    Be aware that getting char * from jstring eats memory that you must free before returning from native with env->ReleaseStringUTFChars().
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Question about public void characters(char []ch, int start, int length) thr

    Can anyone tell me how you would keep all the characters from within one pair of tags together each time characters() is called?
    the character() method doesn't read all the character data at once, and i would like to create a buffer of what it has read.
    thank you.

    I recommend using getNodeName and/or/combination
    with
    getNodeValue to get the strings.i have tried using a buffer, and i have got that to work!
    At long last I am seeing the string I would like to see as it is in the xml file!
    thank you for your help
    here is an example of my code:
    private boolean isDescription = false;
    StringBuffer sb = new StringBuffer();
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
         if(localName.equals("description")) {
              sb = new StringBuffer();
              isDescription = true;
    public void characters(char []ch, int start, int length) throws SAXException {
         if(isDescription == true) {
              String desc = new String(ch, start, length);
              sb.append(desc);
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
         if(localName.equals("description")) {
              isDescription == false;
              System.out.println(sb.toString());
    }

  • How to user bufferedreader.read(char[] cbuf,int off,int len)

    how to user bufferedreader.read(char[] cbuf,int off,int len)?
    can you give me an sample code? i dont understand the use of offset here... thanks!

    The offset simply gives you the ability to read in data starting at some point in the buffer other than the first character.
    If, for example, you are reading from some stream that is being filled slower than you are reading, then you can write a read loop that will fill the buffer with successive reads - the first at character 0, then next offset past the end of the first set of data, etc.

  • Puzzel- Method to optimize int[ ][ ]of int[ ][ ]'s

    I'm righting a class that takes an int[][], initially filled with 0 or null. A rectangular block is added to the int[][] with a value of 1 - 9. Another rectangular block is added to the int[][] with a different value from the first, usually the next int (2). The current setting is
    final static int width = 47;
    final static int height = 3800;
    public int[][] grid = new int[47][3800];
    I have a method addBlock(int blockWidth, int blockHeight) which adds a block to the grid. However it doesn't properly optimize the grid to fit the most efficient number of blocks. It simply adds a block to the top left corner of the grid (where the top left is grid[0][0]) and checks to make sure the next block does not overflow the width bound of 47. If it does it simply places the block at the first available grid where the grid value = 0 and does not overflow the bounds. It doesn't check to make sure that it isn't overriding other blocks, and it doesn't move existing block to make room for new blocks.
    What I am looking for is a way to keep individual blocks together. There could come a situation where two blocks with the same value touch. In this case what would happen?
    Also how do you move blocks once they have been added to the grid?
    Finally, is there an existing algarythm for doing a best fit on a grid of grids?
    Suggestions, other places to look, code, websites to check or any valuable information would be greatly appreciated.
    If needed I could post the existing source, which stacks the grids from the upper left corner and works down. I have a viewer written already which reads in an int[][] and displays it in 9 colors, however I can easily expand the color recognition to as many colors as needed.

    Hi again,
    even if the sequential adding of block will not lead to an optimized layout of the templates on the bolt, we can improve the algorithm given by robert19791 - starting from the top left position of the bolt working towards the right bottom we can say the following:
    The top-left corner of each rectangle is on the right or bottom edge of another rectangle except for the first one added. So, when we search for a position, we will do it along these edges of all the rectangles added before. The test, if it fits in a certain position, can be done with an intersection check with all other already added rectangles, which right or bottom edges are not left or top of the tested position. Ofcourse the rectangle to add must be inside the bolt.
    This will reduce the postions to check and also give a solution for the overlapping problem Robert has mentioned. In those cases where the fit test fails, it should be tested, how large the overlapping area is - if it is not so big, it is perhaps a good idea to shift the rectangle, which would be overlapped otherwise. During such a shift operation the rule of the top-left-corner sitting on the right or bottom edge of another rectangle can be broken, instead the rule is, that an edge is in contact with another rectangle's edge.
    Ok, this for now
    greetings Marsian

  • Can't convert int to int[]    URGENT!

    HI list!
    Here is the code part I have:
    /** Holds the permutated groups which are arrays of integers. */
    Vector v;
    if(arg.length<3) System.exit (0); //Need at least 3 arguments
    elements=new int[arg.length-1]; //Create array to hold elements
    /* Copy the arguments into the element array. */
    for(i=0;i<arg.length-1;i++) elements=Integer.parseInt(arg[i+1]);
    groupsize=Integer.parseInt(arg[0]); //Get the number in each group.
    calc(groupsize,elements.length); //Find out how many permutations there are.
    v=permutate(groupsize,elements); //Do the permutation
    for(i=0;i<v.size();i++) { //Print out the result
    elements=(int[]) v.get(i);
    System.out.println("");
    for(j=0;j<elements.length;j++) System.out.print(elements
    [j]+" ");
    System.out.println("\nTotal permutations = "+v.size());
    and the error I get is:
    Permutate.java:26: Incompatible type for =. Can't convert int to int[]. for(i=0;i<arg.length-1;i++) elements=Integer.parseInt(arg[i+1]);

    elements=(int[]) v.get(i);
    The above statement is illegal. Its not possible to
    use an array type in casting at all.
    Basically you are trying to do a kind of multiple
    casting, which is not possible.
    You might want to store vectors in vector v instead of
    arrays in vector v. Its better to do this.There's nothing wrong with the line
    elements = (int[]) v.get(i);
    assuming the returned object from the vector is in fact an array of int. Even then, it is a runtime exception condition not a compile-time error.
    Using arrays is perfectly valid where you know the size of the array at creation and do not need to resize it during use... The arrays are fast and compact. This is however not a comment on the approach taken in this case but merely an observation that arrays are not always an inferior choice to one of the collection classes.
    Now put up your dukes... ;)

  • Public int indexOf(int ch, int fromIdx) doesn't work for some decimal value

    I have tested two strings below. These two string are identical, except the first string has a char 'ƒ, which has a decimal value of 131. And the second string has a char 'À', which has a decimal value of 192. I am expecting the same output, which is 11, but I got -1 for the test I did using the first string. In the API for public int indexOf(int ch, int fromIndex), values of ch is in the range from 0 to 0xFFFF (inclusive). It is highly appreciated if anyone could provide any insights on why -1 returned when the first string is tested. Thank you in advance.
    String strHasDecimal131 = "Test value ƒ, it has a decimal value of 131";
    String strHasDecimal192 = "Test value À, it has a decimal value of 192";
    int badDecimal = 131;
    int idxBadDecimal = strHasDecimal131.indexOf( badDecimal, 0);
    System.out.println( "index of Bad Decimal: " + idxBadDecimal );
    The output is: index of Bad Decimal -1
    int badDecimal = 192;
    int idxBadDecimal = strHasDecimal192.indexOf( badDecimal, 0);
    System.out.println( "index of Bad Decimal: " + idxBadDecimal );
    The output is: index of Bad Decimal: 11

    Thank you everyone for your inputs. Following are the print statements and the output: for character 'ƒ' and ' À':
    System.out.println((int)'\u0083' + ", " + (int)'\u00C0' + " print decimal value" );
    Output: 131, 192 print decimal value
    System.out.println((char)'\u0083' + ", " + (char)'\u00C0' + " print character value" );
    Output: ?, À print character value
    According to Latin-1 Supplement table, the first char in the output would have the appearance of ƒ, instead it has the appearance of ?
    System.out.println( (int)'ƒ' + ", "+ (int)'À' + " print integer value");
    Output: 402, 192 print integer value
    I also have tried to print out the decimal value of following char: € &#65533; ‚ ƒ „ … † ‡, according to Latin-1 Supplement table, these char has decimal value of 128 through 135 and hex value 0x0080 through 0x0087. And the output from java println is: 8364 65533 8218 8222 8230 402 8224 8225.
    System.out.println((int)'€' + " " + (int)'&#65533;' + " " + (int)'‚' + " " +(int)'„' + " " + (int)'…' + " " +(int)'ƒ' + " " + (int)'†' + " " + (int)'‡' );
    As I did before, I print out character of decimal value of 128 through 135
    System.out.println((char)128 + " " + (char)129 + " " + (char)130 + " " +(char)131 + " " + (char)132 + " " +(char)133 + " " + (char)134 + " " + (char)135 );
    Output: ? ? ? ? ? ? ? ?
    Not sure why java prints character for decimal value of 128 through 159 differently from what appears in the Latin-1 Supplement table. Any of your inputs are appreciately.

  • SetRowHeight(int row,int height) fails

    Hi!
    I have a problem when I want to resize a row to a new height using the method
    setRowHeight(int row,int height)this don't do nothing!
    I'm using the example of a special JTable to merge cells wich is avaiable in http://codeguru.earthweb.com/java/articles/139.shtml
    I try to look in the code of this example, but I can't find the problem.
    I'm not using the merge method in the row I want to resize, so it should be working.
    If someone could help me I appreciate.
    Thanks in advance.

    From what I can tell from looking at the source code for MultiSpanCellTableUI, the paint method draws the cell rects using the general row height (ie table.getRowHeight() ) instead of the row specific height (table.getRowHeight(int row)). This could be a design flaw. You could attempt making the change in source code and see what results you get.
    ICE

  • Driver not support createStatement(int resultSetType, int resultSetConcurrency)

    I am using Weblogic 5.1 for development and would like to use resultset for
    updating
    when I call 'createStatement(int resultSetType, int resultSetConcurrency)'
    Error message prompt out that the driver does not support such method
    I am using weblogic.jdbc20.oci.Driver & weblogic.jdbc.mssqlserver4.Driver
    for testing
    Can some body help me
    regards,
    Fannie

    Right, this method is not implemented by weblogic driver. You should get a
    message like,
    "This JDBC 2.0 method is not implemented" from
    weblogic.jdbc20.oci.Connection or weblogic.jdbc20.mssqlserver4.TdsConnection
    "Fannie" <[email protected]> wrote in message
    news:3b563e73$[email protected]..
    I am using Weblogic 5.1 for development and would like to use resultsetfor
    updating
    when I call 'createStatement(int resultSetType, int resultSetConcurrency)'
    Error message prompt out that the driver does not support such method
    I am using weblogic.jdbc20.oci.Driver & weblogic.jdbc.mssqlserver4.Driver
    for testing
    Can some body help me
    regards,
    Fannie

  • StringBuffer delete (int start, int end) is too slow - HELP!!

    I've traced a performance problem to one line of code: a StringBuffer "delete (int start, int end)" call.
    My StringBuffer contains a lot of data, maybe 500,000 characters. My code loops, searching for a particular character (the search is fast, btw). When that particular character is found, I want to delete that character plus the characters following it. My code looks something like:
    int pos;
    while ((pos = myStringBuffer.toString().indexOf('~')) != -1)
         myStringBuffer.delete(pos, pos + 3);I'm 100% sure the bottleneck is the delete call. If I change the code to the following, where I've commented out the delete, it loops the same number of times and runs lightning-fast:
    int pos = 0;
    while ((pos = myStringBuffer.toString().indexOf('~', pos)) != -1)
         pos++;The loop, and therefore the delete method, will be executed thousands of times. To give you some idea of the poor performance I'm seeing, the first code (with the delete) takes more than 10 minutes to run, while the second code (without the delete) takes less than one-tenth of one second.
    Any suggestions? Thank you.

    Thanks for all the help! da.futt's solution works perfectly: 65,000 times through the loop in about one-tenth of one second. Problem solved.
    We're still on Java 1.3.1 (large company = slow to upgrade), so I can't even remove toString(). I wonder how many "small" performance problems we have (and are unaware of) that could be addressed by upgrading?
    My first posting here and a positive result. I'm not a Java expert, but neither am I a rookie. I'll be back to try and help others as I've been helped here.
    Thanks again.

  • How to solve this Error?? Error_2_Cannot implicitly convert type 'int' to 'int[]'_

    Hi,
    I created a structure. It contains 1& 2 diamensional arrays. Now I want to pass values to these arrays. But at that time I got the following error.
        Error 2 Cannot implicitly convert type 'int' to 'int[]' 
            publicstructtest
              [FieldOffset(160)]
             [MarshalAs(UnmanagedType.ByValArray,
    SizeConst = 3)]
            publicint[]
    DC;
               [FieldOffset(168)]
              [MarshalAs(UnmanagedType.ByValArray,
    SizeConst = 8)]
              publicfloat[]
    IN;
               [FieldOffset(176)]
              [MarshalAs(UnmanagedType.ByValArray,
    SizeConst = 8)]
            publicbyte[,]
    us;
    privatevoidbutton1_Click(objectsender,
    EventArgse)
    //int[] T_ADC = new int[3];
                array[0].DC =12
                array[0].IN[0] = 11;
                array[0].us[0, 0] = 1;

    @DAANNIII
    >>I wrote the code as follows
    array[0].DC[0]
    =12
    but after that I got an error that  " Object reference not set to an instance of an object".Why this type of error occurs??
    Still confused about how you define variable "array[0]".
    Based on your error information, because your DC is null. As I said before, DC is an int[]. You must assign an array to DC.
    int[] array = new int[] { 2, 43 };
    test s = new test();
    s.DC = array;
    s.DC[0] = 12;//It works fine
    Best regards,
    Kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Error message:can't convert int to int[]

    Hi,
    I am trying to build a java code which convert decimal to binary
    but ,I found error message "can not convert int to int[]"
    some line of code:
    int i=12,n=0;
    int arr[n]=i%2;
    n++;
    here I am trying to store remainder in an integer arrary.
    how can i do this???????
    guide me please
    -shobhit singh

    shobhit_onprob wrote:
    i want to store some integer values in to an integer arraryDid you follow the address that was given to you? I doubt it, and even if you did, 2 minutes is not enough time to read, let alone understand, the tutorial.
    Go back there, it describes all the basics you need.

Maybe you are looking for

  • Try to add the ESX to n1kv it give the below error got (vim.fault.PlatformConfigFault) exception

    Hello i m sharing to the forum the discussion we had in fb about my issue with a host see below: Guys anyone saw this error when u try to add the ESX to n1kv it give the below error vDS operation failed on host esxXX, An error occurred during host co

  • Netflix on Mexico issue

    Netflix has just became available in Mexico, how can i make it available on my Apple TV 2? I have the most recent firmware version, tho i can't see it anywhere can you guys help me with this issue?

  • I like my layout in DWCS6 (code top - design below) so how do I change my workspace in DWCC?

    I like the cloud option and many of the new features of DWCC but I do not use it often because I cannot find an easy way to configure my workspace to my liking.  The side by side format does not allow me to see the webpage as laid out on the web.  Ei

  • Mailsession Multicast Error!!

              I need to use the javax.mail.Session class to send mail.           It works on a normal WL server, but not on a clustered server.           On the bottom of my cluster weblogic.properties file I have something like           weblogic.resour

  • Media Offline Showing???

    In my timeline some of the clips are showing that red/white Media Offline. The clips are not offline however. Does anyone know why these are randomly popping up on the timeline as "Media Offline"? I am getting a little nervous about this. If I click