简体   繁体   中英

Java 7 - Pairing two ArrayLists

I have an existing int array, say int[] pageNumbers = {1, 2, 3} .

I'm writing some code to review these pages and define what each page is via another int, eg;

Page Types:

  • 25 = Front Page
  • 50 = Contents
  • 75 = Index

What i'm trying to work out is that when my code has determined what the page is, how do i pair the page and the page type into an array.

For example, if page 1 was Front Page, page 50 was Contents and page 75 was Index, i'd want something like the below defining:

int[] pagesAndTypes = {(1, 25), (2, 50), (3, 75)};

Finally, once I had this array, how could I get at the values? Such as, I want see what page the Index is on, so I'd write a method to find the Index in the array and then the adjoining value, which would be the page number.

Instead of using an array, you could use a Map (maybe also an ArrayList depending on how you plan on indexing). In a Map, each value is "mapped" to a key, and values are accessed via referencing the key.

For your problem it would look something like this:

Map<Int, Int> map = new HashMap<Int, Int>();
map.put(1, 25);
map.put(2, 50);
map.put(3, 75);

Check out the javadoc tutorial for help working with Maps

You can use a Map to associate one key with one value. In the code below, I use a Map that stores keys as integers and values as Strings.

Map<Integer, String> pageMap = new HashMap<>();

// inserting data
pageMap.put( 25, "Front Page" );
pageMap.put( 50, "Contents" );
pageMap.put( 75, "Index" );


// getting data
// will print Front Page
System.out.println( pageMap.get( 25 ) );

To iterate over a Map, ie, to see what it have stored, you can do something like this:

for ( Map.Entry<Integer, String> e : pageMap.entrySet() ) {
    System.out.printf( "%d -> %s\n", e.getKey(), e.getValue() );
}

If you need to preserve the insertion order, you will need to use a Linked implementation of the Map interface, like a LinkedHashMap. For example:

Map<Integer, String> pageMap = new LinkedHashMap<>();

When you iterate over this map, all the data will be presented in the insertion order, since it will preserve this order. For the HashMap presented above, the order will be dependant of how the HashMap will store the data (based on the hashCode of the key objects).

So, use a HashMap if you don't care about the order of the inserted data. Use an LinkedHashMap if you want to preserve the insertion order. There data will be stored. There are lots of different implementations for the Map interface. These implementations, for Java 7, can be found here https://docs.oracle.com/javase/7/docs/api/java/util/Map.html

Specifically for your problem, you will have something like:

int[] pageNumbers = {1, 25, 3};
Map<Integer, String> pageMap = new HashMap<>();

// inserting data
pageMap.put( 25, "Front Page" );
pageMap.put( 50, "Contents" );
pageMap.put( 75, "Index" );

for ( int page : pageNumbers ) {
    String pageTitle = pageMap.get( page );
    if ( pageTitle != null ) {
        System.out.printf( "The title of the page %d is %s\n", page, pageTitle );
    } else {
        System.out.printf( "There is not a page title for the page %d\n", page );
    }
}

As already said, you can also create an specialized class to group this data (page number with title or any other thing), stores it in a List and them iterate over this list to find the page you want, but for your problem, at least for me, it seems to be a better aprroach to use an Map.

What i'm trying to work out is that when my code has determined what the page is, how do i pair the page and the page type

You usually do this by creating a custom class holding the information which belong together:

class DocumentPart{
   int startPage;
   int partType;
}

Then you can create an array (or better a collection like a List ):

 List<DocumentPart> documentParts = new ArrayList<>();

There you can put your Elements:

     DocumentPart documentPart = new DocumentPart();
     documentPart.startPage=1;
     documentPart.partType=25;
     documentParts.add(documentPart);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM