2

I was hoping someone could help me out with a small problem I am having in java. I have a List and an ArrayList that I would like to output to a file with the first element of each, printed next to one another. Here is what I have so far:

List<String> uniqueList = new ArrayList<String>(dupMap.values());
for(String unique:uniqueList){
   out.write(unique + "\r");
}
ArrayList<Integer>subtr=new ArrayList<Integer>();
out.write("The number: " + subtr + "\r");

This results in this output:

A
B
C
D
E
F
The number: [1, 2, 3, 4, 5, 6]

But I would rather it be like this:

A The number: 1
B The number: 2

...etc.

I am not really sure where to start on how to get the output format like that. I tried putting both sets of values into arrays but I just ended up confusing myself... Which is how I ended up here. Any help would be super appreciated.

1
  • ... are you sure \r is right? Commented Aug 28, 2012 at 19:11

5 Answers 5

6

Simply do this:

String br = System.getProperty("line.separator");
for (int i = 0, n = Math.min(uniqueList.size(), subtr.size()); i < n; i++)
    out.write(uniqueList.get(i) + " The number: " + subtr.get(i) + br);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I thought that it wouldn't be too complicated but I kept getting lost. Do you mind explaining Math.min() as it is used here for me?
@User Sure! it's just a safeguard in case one of the lists is longer than the other, in this way we won't try to access an index outside of the shorter list. If both lists are guaranteed to always have the same size, then n = uniqueList.size() would be enough.
1

Something like this:

List<String> uniqueList = new ArrayList<String>(dupMap.values());
ArrayList<Integer> subtr = new ArrayList<Integer>();
int length = Math.min(uniqueList.size(), substr.size());
for (int i = 0; i < length; i++) {
   out.write(uniqueList.get(i) + " The number: " + substr.get(i) + "\r");
}

Comments

1
List<String> uniqueList = new ArrayList<String>(dupMap.values());
ArrayList<Integer>subtr=new ArrayList<Integer>();

for (int i = 0; i < uniqueList.size(); i++){
   out.write(uniqueList.get(i) + "\r");
   out.write("The number: " + subtr.get(i) + "\n");
}

NOTE: This assumes that both lists have the same number of elements. If they are not, you would iterate to Math.min(uniqueList.size(), subtr.size()).

3 Comments

That will give you an exception, if subtr.length() < uniqueList.length()
@Baz Yes. But the question assumes that they have the same number of elements.
They indeed should be the same size, I forgot to specify that. Sorry.
1

This should do it:

int maxLength = Math.min(uniqueList.size(), subtr.size());
for(int i = 0; i < maxLength; i++)
{
    out.print(uniqueList.get(i) + " The number: " + subtr.get(i) + "\r");
}

4 Comments

You should remove "\r" and replace with new line or println.
@Desolator Could you explain why?
Some environments don't accept \r as a new line specifier, \r is the carriage return, not new line. It would be replaced with \n. However, best of all, using println is the most portable.
@Desolator Fair enough, but the OP used "\r", so I guess it's only appropriate, if I provide code, that does the same as his.
1

The efficient way is to maintain a HashMap with uniqueList as keys and subtr as values. Then iterate over a map.

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

for (int i = 0; i < uniqueList.size(); i++){
    map.put(key, subtr.get(i));
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    out.println(entry.getKey() + " The Number : " + entry.getValue());
}

Comments

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.