Replace integers inside of an array

Hi,
I'm currently trying to replace integers inside of a multidimensional array, but I don't know how to make it so that it doesn't skip them (if they've been changed, I assume). Here's an example, hopefully you understand, if not please let me know and I'll try to go further in depth.
public class Example {
     private int[][] replaced = new int[][] {
          {0, 1}, {1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 2}, {6, 0}
     public static void main(String[] var) {
          new Example().replace();
      * Should replace the integer inside of the first index of replaced with the second, without skipping any
     public void replace() {
          for (int a = 0; a < 6; a++) {
               if (a == replaced[a][0]) {
                    a = replaced[a][1];
                    System.out.println("replaced " + replaced[a][0] + " with " + replaced[a][1]);
}

sharkura wrote:
More specifically, your replace function is doing this ... if the value at a,0 is equal to the current array index a, set the array index a to the value at a,1.
This does not replace the value at a,0 with the value at a,1. With your specific data, it updates the loop index prematurely, so one or more values are skipped. Without running it, it may cause your loop to execute without termination. With some data, it will definitely do that.
This will make your loop behave very strangely.  It is almost never correct to modify a loop index variable inside the loop.
To repeat:
It is almost never correct to modify a loop index variable inside the loop.
¦ {Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Changing the size of list of random integers inside an array

    Hi. This is a small part of a bigger project about searching algorithms. Anyway, I need help with creating an array containing random integers. First, the array will generate a random list of 50 integers, then it will generate 51, etc. I know I have to put it in a for loop, but I don't know how to set up the array to get bigger after each pass.
    Here's what I have so far:
    for (int n=50; n<=500; n++){
    // Random number generator.
    Random generator = new Random();
    // Numbers in the range of 10 to 99.
    int randomNumber = 10 + generator.nextInt(99);
    /*This is the array that will contain the list of random
    numbers. Currently, it only generates 1 random
    number. How do I use the "n" in my for loop to control
    the size of my list of random numbers?*/
    int aa[] = {randomNumber};
    }Thanks!

    Array Size cannot be dynamically assigned, elseu
    need to use Vectors.The OP can create a new array each time.
    O_o
    Why would anyone want to do that?
    Vector is definitely the way to go!!Read the original question again:
    "This is a small part of a bigger project about searching algorithms"
    So the task is probably to test or implement seach algorithms that are searching within arrays. You can't use a Vector in that case.
    You probably also want to create new random data for each test, so the OP should do what he is trying to do. Create arrays of different lengths with different test data.
    >
    Or u can try tis logic..
    Create a string buffer put all the generatednumbers
    in the string seperated by a dash.
    Use string tokenizer and get the individualstrings
    and type cast them back to int(if you want)O_o
    Why would anyone want to do that?
    Consider it as a challenge of doin without
    collections and minimum memory utilization :-)System.arraycopy
    Kaj

  • Search and replace elements in a 2D Array

    Hello,
    i have a programming problem in Labview 7.1 under Win XP. I want replace elements in a 2D Array. The code works fine but I want replace elements in the 2D Array (for example all elements between 2 and 5 should replace to zero).
    Thank you in advance
    Rene
    Attachments:
    Search_and_replace 2D Array.vi ‏42 KB

    Sometimes, you don't need any loops
    Note to Devchander:
    You can configure the "In Range and Coerce" finction to either exclude or include the limits. This is done by right-clicking on their respecive terminal. Try it! The terminal will be either a solid or open diamond, depending on configuration.
    Message Edited by altenbach on 01-11-2006 07:59 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    Search_and_replace_2D_ArrayMOD.vi ‏24 KB
    SearchReplace2D.png ‏3 KB

  • Has anyone replaced the inside piece to the charger on a Satellite?

    Has anyone replaced the inside piece to the charger on a Satellite?  I broke it and I feel like I may be ab le to fix it if I can get the overtorqued screws out.  I would also have to find the proper part.
    Any help would be appreciated!

    I haven't done it but you can go here to compare
    temps of people who have done the fix.
    If you look at the values, they vary extremely.
    The dual core chip set is designed to run hot... The case, on the other hand, is another story.
    For what it is worth, I think all youse guyz are focusing on the wrong issue... the case design. The chips are meant to be quite warm as they do their job, but the case's exhaust goes in FRONT of the LCD screen, just above your function keys.
    The keyboard area above the power inverter and airport card is not thermally shielded, so heat goes up, through, and past the keys (QWE ASD area). The bottom of the case plastic has no thermal layering, hence you get hot legs.
    Elevating the back of the MacBook helps tremendously.
    Knocking back the duocore CPU temperature by an across-the-board 10 - 15 degree hit will still result in a very hot computer.
    It is the case.... The case is the problem child.

  • Bit permutation inside a byte array

    Hi everyone,
    I'm trying to implement DES algorithm, i want to permute specified bits inside an 8byte array.
    I tought about extract every bits from the byte array inside a 64 byte array and then do the permutation.
    But i don't see how to put these bits back into an 8byte array to return the result of encryption/decryption.
    This is what i've done so far (inspired by sources i found on the internet) :
    // extends 8 byte array into a 64 byte array
    public static byte[] extendsByteArray(byte[] bytes) {
    byte[] tab = bytes;
    short len = (short)(desBlockSize * (short)8);
    byte[] bits = new byte[len];
    short i,j;
    for (i = (short)(desBlockSize - (short)1); i >=0; i--) {
    for (j = (short)0; j < desBlockSize; j++)
    bits[--len] = (byte) ((tab[i] >> j) & 0x01);
    return bits;
    // 64 byte array to 8 byte array
    public static byte[] transformByteArray(byte[] bits){
    byte[] tab = bits;
    byte[] bytes = new byte[desBlockSize];
    short i,j = (short)0;
    for ( i = (short)0; i < (short)8; i++){
    bytes[i] = (byte)((bits[j] * (short)2^7) + (bits[j+(short)1] * (short)2^6)+
                   (bits[j+(short)2] * (short)2^5) + (bits[j+(short)3] * (short)2^4)
                   + (bits[j+(short)4] * (short)2^3) + (bits[j+(short)5] * (short)2^2)
                   + (bits[j+(short)6] * (short)2) + (bits[j+(short)7]));
    j +=(short)7;
    return bits;
    This implementation doesn't seems to work, do u have some ideas why it doesn't work or is there an easier solution to permute bits as i'm stuck with this fo the moment.
    Thanks in advance for your answers,
    Julien

    If you are using JavaCard please do not use multiplications as you used in your code. Integer multiplications are very slow in any platform that does not implement them in hardware, and the virtual machine that runs in JavaCards usually does not do any optimizations or Just-In-Time compiling - simply interprets the codes. Memory and CPU power are at a premium in Java Cards. Even if your Java Card has a hardware multiplier, usually it is reserved for RSA operations - it is not available for general use for Java programs.
    Instead of using x * 2 raised to the sixth power, use x << 6. It is faster and generates fewer bytecodes.

  • Why is it so difficult to replace one element in an array?

    Maybe I am missing something but it seems overly complicated to replace one element in an array.
    I have a 3 Col 15 row array. Each column represents a board of IO and each row is a specic IO location on the board.
    I have 4 arrays each that vary in lenth that represent 4 different IO types (i.e. AO, DO, AI, DI). I want to people to write them one by one in order to allow a map to be stored as a constant. In that way if the end user decides to change some of the IO around on me it is easy.
    However I am not seeing an easy way to write to one specific element. Replace supset will only allow me to call out either row or column.. not both (this makes no sence to me).
    I hope someone can help.
    Thank you.

    You can certainly replace a single element in a 2D array and specify row and column. Attach the code you are having problems with.
    Attachments:
    Replace Element.PNG ‏2 KB

  • I have an ipod nano and its fall down , the inside screen is broken..... can APPLE replace this(inside screen)???!

    i have an ipod nano and its fall down , the inside screen is broken..... can APPLE replace this(inside screen)???!

    Hello Alia A,
    Sorry to hear about your iPod nano screen.  I recommend reviewing the following link for service options (you can update the country selection to your location after accessing the link):
    Service Answer Center - iPod
    http://support.apple.com/kb/index?page=servicefaq&geo=United_States&product=ipod
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

  • Looking for way to replace " with \" inside of coldFusion created array for JSFL

    I am trying to figure out the best way to fix data coming from a database table through the use of ColdFusion, in which there are some quotation marks that JavaScript Flash doesn't want in the array before adding the text content to a quiz.
    At the moment, if there are any quotation marks in the elements of the array, even though each element is surrounded by quotation marks, I have to add a backslash to escape them.
    I was wondering what would be the regular expression to use to do this, not for the quotation marks surrounding each element, but for those used inside of them.
    For example,
    ["08 Working with Flash Forms_16",
    "The syntax for the submit button was .",
    "name="submit" type="submit"",
    "name="submit"action="submit"",
    "id="submit" type="submit"",
    "id="submit"action="submit"",
    "No, name="submit" type="submit" is correct.",
    "true",
    "false",
    "false",
    "false",
    "897"] ,
    is an example of an element created that has quotation marks inside of the quotation marks for each element.
    I am debating about handling this either with XML by writing code to do that with ColdFusion instead of using the code I generated with ColdFusion to create this.
    I am also debating about writing the regular expression in a way that I could use it from either ActionScript 2 or 3.0, going at it at the file level maybe.
    At the moment I am opening the file and experimenting with writing a Find expression that uses regular expression to select what I want to change before changing it.

    I figured this out already. You can use the either the ColdFusion function, Replace, or REReplace if you want to use regular expressions. But I just wrote an actionscript prototype function inside the superclass called replace to fix it using split and join array methods.

  • Search and replace string inside a column

    Hi,
    in my table there is Column A with Type NVARCHAR.
    i need to search inside all the rows in that column to find the string "_TH" and remove it wherever it exist.
    how can i do it?

    you can follow below solution .
    Step 1: Create a temporary table to load all the records which are having column with '-TH'
    Step 2 :  Use update statement and join it with temporary table to update the qualified records
    Benefits: This is a set based solution which is optimal and high perfroming
    Script:
    create table test1(col1 nvarchar(20))
    insert into test1 values('one_th'),('two_th'),('Gauri')
    --Create Temporary table
    select * 
    into #tmp2
    from Test1
    where col1 like '%_TH%'
    --update all such rows which are having column with '_TH'
    Note: I am replacing all columns with '_TH' with space . you can replace with other characters based upon your requirement.
    update t
    set col1 = replace(t.col1,'_TH','')
    from test1 t
    inner join #tmp2 t2
    on t.col1 = t2.col1
    Please comment If this solution helps you.

  • Event handler elements inside of the array

    I have an array of clusters, of which inside the cluster are buttons.   I want to be able to tie an event handler to these buttons ( they would all handle the event the same, the only difference would be the index they were handling) 
    A while back a similiar problem was posted here:
    http://forums.ni.com/t5/LabVIEW/Array-of-Clusters-with-Graph-Y-Scale-Change-Event/td-p/1194181/page/...
    However, this solution did not appear to be able to deliver the index of which button in the array was pressed .. this is critical for my application.   The button is basically an "edit' buttonw which allows the user to edit the elements of the cluster through a popup.   Obviously I need to know which cluster the user intends to edit.

    nathand wrote:
    One option: when the code starts, build an array of references to the edit buttons inside each cluster. Then, in the event case, search that array for the reference that triggered the event. The index at which the reference is found tells you which cluster to edit.
    This might work, but it should be possible for the user to add/delete elements from the array so I would need to be able to do this even after program initialization.
    Currently, I have just made dozen buttons alongside the array of clusters.  But this workaround won't allow past a fixed number at program initialization.

  • Replace Hard Disk ST336607LSUNG36G on Array Controller

    Hi,
    There is a Sun Fire V65x server that runs Linux Red Hat Enterprise Edition 4.
    This server has an Intel SRCZCR Array Controller.
    The front panel has the disk light and warning light red. Also, one of the disks has a red/orange light instead of a green light.
    The array controller advices there's one missing private hot fix drives missing, but the array also says SMART information is good for every hard disk.
    I'd like to know how should I proceed to replace one of the disk.
    From my point of view, and after reading the manuals, I have to shutdown the server, replace the hard disk, power up, enter the array configuration, choose I've done it always on Windows machines, but not on linux and Sun.
    Repair Array Drives, and ask it to add the new hot fix drive. And that's all.
    I understand there should not be do nothing at OS level, because is a Hardware array.
    Am I correct?
    I've done it always on Windows machines, but not on linux and Sun.
    Thanks to everyone.
    Alex.

    Hi,
    In your first post you state that the Hard Drive was failing, what led you to believe this?  Ubuntu seems to be easier on the HDD than Windows, so if there is a problem with the drive, it may just not be showing up yet.
    Certainly, if there is a problem with the HDD, this would cause the failed recovery and buying a replacement set of Recovery Discs would be of no help.
    Check the Hard Drive as follows.  Shutdown the notebook.  Tap away at f10 as you start the notebook to enter the bios menu.  Under the Advanced or Diagnostic tab you should find the facility to run tests on both the Hard Drive.  Post back with the details of any error messages.
    Regards,
    DP-K
    ****Click the White thumb to say thanks****
    ****Please mark Accept As Solution if it solves your problem****
    ****I don't work for HP****
    Microsoft MVP - Windows Experience

  • How to replace cluster element in an array?

    hi,
    I have understood preferable choice is to initialise array first and replace elements in an array instead of inserting new element in an array e.g. in a while loop.
    So I started to this evaluate since I have an application where I want to read lots of measurements from a txt file and display them in a XY graph. XY graph must show Y value and corresponding X value either red if it is out of range and in green if it is in range. I found a solution to do it with an array of clusters.
    In attached example I have two different methdologies represented. My problem is that upper solution doesn't display content of all measurements. Can somebody tell me what I'm doing wrong ?
    regards,
    petri
    Solved!
    Go to Solution.
    Attachments:
    plot test 3.vi ‏19 KB

    Petri wrote:
    ... using cluster with three elements was a method I found to put red and green dots in one XY graph, eg. Y=coordinateY, X(green)=NaN,X(red)=coordinateX. Perhaps there is a better way to display the data, but I couldn't find it.
    That is an odd thing and makes little sense. Why three? four, five, etc are NOT supported, so what if you want more than one color? (in range, fail high, fail low?). For more detail see the discussion here.
    In any case, reading all these values from a datafile and creating xy plots is trivial. I would recommend to use complex data. a single complex array will graph IM vs RE. For multiple plots, combine them using "build cluster array". No loops needed.
    Here's a quick draft. Of course you need to decide if the data is in columns or rows, etc. so modify as needed.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    plot2XY.PNG ‏7 KB

  • How to Replace GUID inside table CRMD_PARTNER

    Hi,
    How to replace old user GUID with a New User GUID inside the table CRMD_PARTNER ?? and from where can we get the GUID of a particular partner ??

    Hi Rahul,
    The CRMD_PARTNER table stores the partner details that are maintained in a document say shopping cart as example (Header and Line Items).
    Now to get all the partners of a the shopping cart use BBP_PD_SC_GETDETAIL function module. This will give all the details + partner details in table form.
    Now you can use the FM BBP_PD_SC_UPDATE to change the details. You can delete an existing partner by setting the deletion indicator (DEL_IND I guess) in the partner table. If you wish to add a new partner add the partner id, partner function and guid of header / item (where the partner is required) as P_GUID.
    Once the above function (BBP_PD_SC_UPDATE) call is success call the BBP_PD_SC_SAVE to save the changes.
    Deleting data from SAP tables is not advisable.
    Note: Once partners are assigned to a document and ordered, then you have to delete that partner and assign a new one if at all they need to be changed. Just replacing the partner guid will cause data inconsistency because except the guid all other data will still be that of the old partner. The above steps explain the procedure for the same
    Hope this solves your problem.
    The below code will delete the existing vendor and add a new vendor for a PO
    REPORT  zkb_partner_chg.
    DATA: lt_e_item     TYPE TABLE OF bbp_pds_po_item_d.
    DATA: lt_e_partner  TYPE TABLE OF bbp_pds_partner.
    DATA: lt_e_messages TYPE TABLE OF bbp_pds_messages.
    DATA: lt_i_item     TYPE TABLE OF bbp_pds_po_item_icu.
    DATA: lt_i_partner  TYPE TABLE OF bbp_pds_partner.
    DATA: ls_e_header   TYPE bbp_pds_po_header_d.
    DATA: ls_e_item     TYPE bbp_pds_po_item_d.
    DATA: ls_e_partner  TYPE bbp_pds_partner.
    DATA: ls_i_item     TYPE bbp_pds_po_item_icu.
    DATA: ls_i_header   TYPE bbp_pds_po_header_u .
    DATA: ls_but000     TYPE but000.
    DATA: lv_e_changed TYPE  xfeld.
    FIELD-SYMBOLS: <fs_partner> TYPE bbp_pds_partner.
    CALL FUNCTION 'BBP_PD_PO_GETDETAIL'
      EXPORTING
        i_object_id     = '3200000576'
        i_with_itemdata = 'X'
      IMPORTING
        e_header        = ls_e_header
      TABLES
        e_item          = lt_e_item
        e_partner       = lt_e_partner
        e_messages      = lt_e_messages.
    * Populate Header Data and Item data
    MOVE-CORRESPONDING ls_e_header TO ls_i_header.
    LOOP AT lt_e_item INTO ls_e_item .
      MOVE-CORRESPONDING ls_e_item TO ls_i_item.
      APPEND ls_i_item TO lt_i_item.
    ENDLOOP.
    LOOP AT lt_e_partner INTO ls_e_partner.
      APPEND ls_e_partner TO lt_i_partner.
    ENDLOOP.
    ** Delete Partner
    READ TABLE lt_i_partner ASSIGNING <fs_partner>
         WITH KEY partner_fct = '00000019'
                  p_guid = ls_i_header-guid.
    IF sy-subrc EQ 0.
      <fs_partner>-del_ind = 'X'.
    ENDIF.
    UNASSIGN <fs_partner>.
    ** Add Partner
    SELECT SINGLE * FROM but000 INTO ls_but000 WHERE partner = '0087000004'.
    CLEAR ls_e_partner.
    ls_e_partner-partner_guid = 1.
    ls_e_partner-partner_no = ls_but000-partner_guid.
    ls_e_partner-partner_fct = '00000019'.
    ls_e_partner-p_guid = ls_i_header-guid.
    APPEND ls_e_partner TO lt_i_partner.
    * Update Doc
    CALL FUNCTION 'BBP_PD_PO_UPDATE'
      EXPORTING
        i_header                = ls_i_header
        i_save                  = 'X'
        iv_with_change_approval = ' '
      IMPORTING
        e_changed               = lv_e_changed
      TABLES
        i_item                  = lt_i_item
        i_partner               = lt_i_partner
        e_messages              = lt_e_messages.
    * Save Doc
    IF NOT lv_e_changed IS INITIAL.
      CALL FUNCTION 'BBP_PD_PO_SAVE'
        EXPORTING
          iv_header_guid = ls_i_header-guid.
      COMMIT WORK AND WAIT.
    ENDIF.
    Regards
    Kathirvel
    Edited by: Kathirvel Balakrishnan on Feb 29, 2008 12:14 PM - Added the code sample for more clarity

  • Can I replace several elements of an array within one time? The number of elements are not fixed

    Hi all,
    I want to replace some elements of a 2D array, but the number of elements are not known beforehand. I dont want to do it with a loop, which may slow down the speed, and the update of elements will not happen simultaneously.  The indexes of elements to be updated are not continuous. I wonder if there is a way to do that efficiently? Do I have to programme it in LabWindows/CVI? Thank you.
    Best wishes,
    Bo
    My blog Let's LabVIEW.

    Use the replace elements function. For non-continuous replacements  of of multiple elements you would nee to iterate at least the number of continous pieces.
    For example:
    1,2,3,4,5,6,7,8,9
    Replace elements 1,2,5,6,8
    You would need 3 iterations in which you can replace (1,2), (5,6) and (8).
    Regards,
    André
    Using whatever version of LV the customer requires. (LV5.1-LV2012) (www.carya.nl)

  • How to use text boxes or rich text boxes inside loops in array format

     Help me,
        I try to use text boxes load automatically showing the character in the text box.One text box showing one character (ex: " A"). I use 5 text boxes.I try to code using   for loop but i don't know how to use array format.
    Please explain any solution.
    I am using Microsoft Visual Basic 2008 Express Edition

    Hi
    And yet again, this version uses a list of textbox names (IN REQUIRED ORDER) to fill.
    ' this is an overly simplified example
    ' in the form of a Project
    ' start a new Project with a default
    ' Form1 with many TextBoxes.A list of
    ' required textboxes to fill IN THE
    ' REQUIRED ORDER is used.
    ' Copy/paste this code to replace
    ' all Form1 code with this.
    ' NOTE: there is plenty of oppertunity
    ' for exceptions as no exception handling is
    ' included here.
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim tbNames As New List(Of String)
    ' add names IN THE ORDER required
    tbNames.AddRange({"TextBox2", "TextBox5", "TextBox1", "TextBox3", "TextBox4"})
    Dim s As String = "boxes"
    Dim counter As Integer = 0
    For Each name As String In tbNames
    For Each c As Control In Me.Controls
    If c.GetType() Is GetType(TextBox) Then
    If c.Name = name Then
    c.Text = s(counter)
    counter += 1
    End If
    End If
    Next
    Next
    End Sub
    End Class
    Regards Les, Livingston, Scotland

Maybe you are looking for