1

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, e.g;

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.

2
  • 1
    Hint : Use a Map. Also, do you mean page 2 was contents and page 3 was index? Commented Oct 18, 2017 at 17:46
  • Either use a Map which realizes pairs like (1 -> 25), (2 -> 50) etc. or use a dedicated Pair class which holds both values and then use something like Pair[] or List<Pair>. Commented Oct 18, 2017 at 17:49

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

While this answer seems to be the closest to what the OP expects, I believe the mapping should be the other way round? map.put(25,1) so that the OP can then say map.get(25) to get the page number for the page that represents the front page?
I was thinking that OP wanted to lookup what page a given section began? I could see being able to do both would be useful, in which case one could use either two Maps which are just flipped, or alternatively using a library with a bidirectional map.
The statement "I want see what page the Index is on" is a clear indication that the OP is primarily looking for the opposite of this answer :). The bidirectional map could be a good additional suggestion. Requirement analysis IMO is one of the most overlooked/undervalued skillset in software development :)
0

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);

3 Comments

Storage is not the only objective here. You also have to think of efficient retrieval. A request such as "get me the page number for the index page" could cost you a bomb if the index page is the last page in the book :)
@CKing Beware of premarture optimization. Always start with a solution along the SOLID principles. Optimize for performance when you have identified the bottleneck by measurement.
Thanks Timothy, this method was most suitable for my needs.
0

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, i.e., 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.

1 Comment

If you read the questionsl carefully, you will notice that 25, 50 and 70 are page types. The OP wants to scan through the pages and map these page types to the page number where the page type appears!! The OP doesn't mention a String mapping anywhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.