Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:35:23+00:00 2026-06-12T16:35:23+00:00

I made a map by arraylist, and trying to swap the index(not the value)

  • 0

I made a map by arraylist, and trying to swap the index(not the value) of arraylist.

For example, the swapRow(1,2) changes the position of row 1 and 2.
This works at the first time, but if I do it again, an error comes out.(Same problem with the swapCol() part.)

Stuck in this problem and need help…

In addition, can anyone help me out making the swapValue() method.
Changing the value is simple, though I also want to move the index, not the value.

import java.text.DecimalFormat;
import java.util.ArrayList;
public class IntMap {

    class Data {
        private int data_;

        public Data(int data)
        {

            setData(data);
        }

        public void setData(int data)
        {
            if(data<0)data=0;
            if(data>99)data=99;

            data_=data;

        }

        public int getData()
        {
            return data_;
        }

    }


    class Index {

        private int index_;

        public Index(int index)
        {
            setIndex(index);
        }

        public void setIndex(int index)
        {
            if(index < 0) index=0;

            index_=index;
        }

        public int getIndex()
        {
            return index_;
        }

    }



    private ArrayList<ArrayList<Data>> datas_;
    private ArrayList<Index> cols_;
    private ArrayList<Index> rows_;

    public IntMap(int rowCount, int colCount)
    {
        if(rowCount < 0) rowCount = 1;
        if(rowCount < 0) colCount = 1;

        cols_ = new ArrayList<Index>();
        rows_ = new ArrayList<Index>();
        datas_ = new ArrayList<ArrayList<Data>>();

        for(int i=0 ; i<rowCount ; ++i) rows_.add(new Index(i));
        for(int i=0 ; i<colCount ; ++i) cols_.add(new Index(i));

        for(int i=0 ; i< rowCount ; ++i)
        {
            ArrayList<Data> temp = new ArrayList<Data>();
            datas_.add(temp);

            for(int j=0 ; j<colCount ; ++j)
            {
                temp.add(new Data(0));
            }

        }

    }

    public int getValue(int row, int col)
    {
        int rIndex = getRowIndex(row);
        int cIndex = getColIndex(col);

        return datas_.get(rIndex).get(cIndex).getData();
    }

    public void setValue(int row, int col, int value)
    {
        int rIndex = getRowIndex(row);
        int cIndex = getColIndex(col);

        datas_.get(rIndex).get(cIndex).setData(value);

    }




    public void setRandomValue()
    {
        for(int row=0 ; row < datas_.size() ; ++row)
        {
            ArrayList<Data> temp = datas_.get(row);

            for(int col=0; col <temp.size() ; ++col)
            {
                Data data = temp.get(col);

                data.setData((int)(Math.random()*100));

            }
        }
    }

    public void log()
    {
        DecimalFormat df = new DecimalFormat("00");
        for(int row=0; row<rows_.size(); ++row)
        {
            for(int col=0; col<cols_.size(); ++col)
            {

                int value = getValue(row, col);
                System.out.print(df.format(value));

                if(col<cols_.size()-1)
                    System.out.print(", ");
            }

            System.out.println();
        }
        System.out.println();
    }

    private int getColIndex(int col)
    {
        if(col <0 || col>=cols_.size())
        {
            System.out.println("[error][IntMap][getColIndex] - col is"+col);
            return 0;
        }

        for(int i =0; i<cols_.size(); ++i)
        {
            if(cols_.get(i).getIndex()==col)
                return i;
        }

        System.out.println("[error][IntMap][getColIndex] - unknown error");
        return 0;

    }

    private int getRowIndex(int row)
    {
        if(row <0 || row>=rows_.size())
        {
            System.out.println("[error][IntMap][getRowIndex] - row is"+row);
            return 0;
        }

        for(int i =0; i<rows_.size(); ++i)
        {
            if(rows_.get(i).getIndex()==row)
                return i;
        }

        System.out.println("[error][IntMap][getRowIndex] - unknown error");
        return 0;

    }

    public void swapValue(int a, int b, int c, int d)
    {

        //

    }

    public void swapCol(int a, int b)
    {
        int col1=a;
        int col2=b;
        int t=a;

        cols_.get(col1).setIndex(col2);
        cols_.get(col2).setIndex(t);

    }

    public void swapRow(int a, int b)
    {   
        int t=a;

        rows_.get(a).setIndex(b);
        rows_.get(b).setIndex(t);

    }

    public static void main(String[] args)
    {
        System.out.println("start!");

        IntMap map =new IntMap(3,4);

        System.out.println("Init map 3x4");
        map.log();

        System.out.println("random map");
        map.setRandomValue();
        map.log();

        System.out.print("get map[0][1]= ");
        System.out.println(map.getValue(0,1));

        System.out.println("set map[2][1]=50");
        map.setValue(2, 1 ,50);
        map.log();

        System.out.println("swap(0,1 <-> 2,1)");
        map.swapValue(0,1,2,1);
        map.log();

        System.out.println("swapRow(0 <-> 2)");
        map.swapRow(0,2);
        map.log();

        System.out.println("swapRow(1 <-> 2)");
        map.swapRow(1,2);
        map.log();

        System.out.println("swapCol(0 <-> 3)");
        map.swapCol(0,3);
        map.log();


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:35:25+00:00Added an answer on June 12, 2026 at 4:35 pm

    I think you got confused the index of rows_ arrayList and your Index type. your swapRow(a,b) should check rows_ list and find out the element (Index Object) .getIndex = a and b, then swap the two Index elements, not just like what you did in your method. If you change the method into followings, it should work. you could do some error handling, e.g. what happend parameter a or b invalid. I just set themt to 0.

    public void swapRow(int a, int b) {
            int ai = 0, bi = 0;
            for (final Index i : rows_) {
                if (i.getIndex() == a) {
                    ai = rows_.indexOf(i);
                }
                if (i.getIndex() == b) {
                    bi = rows_.indexOf(i);
                }
            }
            final Index aidx = rows_.get(ai);
            rows_.set(ai, rows_.get(bi));
            rows_.set(bi, aidx);
    
        }
    

    and, i don’t know your exact requirment. but from the design point of view, there might be room to improve.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Made this nice little loop for hiding and showing div's, works as a charm
I have a UK map made as a flash/swf movie. This map is embeded
Hopefully this will be clear enough. I have a 2d map made of tiles,
I have a map made up of rows and columns of hexagons This isn't
I have made a map of functions. all these functions are void and receive
I have made a map with jQuery which is split up in many regions,
I made a Google Map in which I customize the InfoWindows using the InfoBox
I have made my own map using the google maps api. It is a
I have made a key mapping for Vim's tab functionality as 318 map <C-h>
I've made some custom beans that implement the Map interface for easy access to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.