Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I learned some javascript as my introduction into programming, and now I'm trying to move on to Java and Python and from there to c++, c#, etc.

I've been caught up with schoolwork, so I've had very little chance to learn anything yet, but I have been lurking around SO looking for easy Java questions that I might have asked myself and trying to learn from the answers.

I saw one that was kind of interesting. It was a homework assignment about finding the most efficient order of jobs to do, assuming each job took two days and had to be in sequential order. I could have easily done this in javascript, but I'm having trouble figuring out how in Java. With javascript I would just return a two dimensional array. Each element being an array containing the profit and the days to work. Ignoring whether or not that's a stupid way to return data in javascript, what is the proper way to return data like this in Java?

In other words, the javascript array I would return would look something like:

[ ['1-3-5', 50], ['2-5-8', 90], ['2-4-7', 75] ]

In Java, I would have to convert the integers to strings to start off, and then find a way to concatenate two two dimensional arrays... Needless to say, this is the wrong way to go about it.

share|improve this question
    
javascript is NOT related to java, it might be very loosely based on it but they are very different –  ratchet freak Oct 4 '11 at 11:19
    
If you want to learn Java, the best place to start is the Java Tutorial: download.oracle.com/javase/tutorial, regardless of what other language experience you have. –  Zeke Hansell Oct 4 '11 at 17:46
    
CompSci w/Java at Stanford...see.stanford.edu/see/…... CompSci w/Python at MIT...ocw.mit.edu/courses/electrical-engineering-and-computer-science/… –  Joe Internet Oct 5 '11 at 5:09

5 Answers 5

up vote 7 down vote accepted

Using arrays for everything is a bad idea. Even in JavaScript. But in Java even more so, because arrays are far more narrow in Java.

The elements of your return array are in fact representing results. You should communicate it accordingly:

class Result {
     public int someInt;//no idea what this actually is, but a class will allow you giving it a proper name
     public String someString;//same as with the other field. Also, I suppose String is actually wrong here, since it rather looks like a '-'-separated list of integers, which should rather be represented as an array of integers
     Result(String someString, int someInt) {
           this.someString = someString;
           this.someInt = someInt;
     }
}

And then your return value should look something like:

ArrayList.ofList(new Result('1-3-5', 50), new Result('2-5-8', 90), new Result('2-4-7', 75))
share|improve this answer
    
Yes, I was originally going to go with an array of integers, but there doesn't seem to be a way to append data to an array. How would you go about it? –  mowwwalker Oct 4 '11 at 7:52
1  
@user828584: You'd use a mutable container such as ArrayList<T>. –  Jon Purdy Oct 4 '11 at 9:25

Firstly, I recommend you rid yourself of the notion that Java and Javascript are in any way related. It's a common misconception with newcomers, but apart from the name, and some of the basic syntax, they're about as closely related as C++ and Python.

Now, I'm not quite understanding the requirements of the actual problem (what does the string of numbers represent?) but if you're trying to decide what data structures to use in Java, the place to look is the java.utils package.

share|improve this answer
    
No, my point was that I had learned javascript because it was an easier language to start with than Java. I know they're completely different. I think I used the term data-structures wrong. I want to know how to properly store the data that I showed with the javascript array. –  mowwwalker Oct 4 '11 at 7:26
    
Relevant reading: stackoverflow.com/questions/245062/…. –  back2dos Oct 4 '11 at 7:29

Java is not JavaScript. You have to rid yourself of the JavaScript loose typing habits to appreciate Java's type system.

In loose languages you optimize for easy implementation of the function being called. Because the function can accept and return data in any format, it makes implementing the function without a lot of overhead easy. The downside is that whoever is calling your function has to look at the source code of your function to know what data it accepts and what data it returns. This is efficient for a single programmer (assuming you can remember your functions), and inefficient for a team (everyone has to read all the source code).

In strict languages, like Java, you optimize for easy implementation by the caller of the function. The caller knows what data format the function accepts, and what data format the function returns. The more strictly you define these data formats, the easier things become for the caller. They can rely on the function signature, without looking at the source code, to know how to use the function. This is inefficient for a single programmer, but efficient for a team (people can treat functions like black boxes).

In this specific case you would have to think carefully about how this function could define its input and output so that it becomes a black box. You wouldn't return arrays of strings. You would return lists of objects of a specific (possibly self-declared) type.

share|improve this answer

Needless to say, this is the wrong way to go about it.

Not necessarily. Whatever does the job at hand in an understandable, testable and reliable fashion is a good way to go about it. At the data structure abstraction level, one can think more-or-less language-agnosticly.

Now, languages with a strong type system like Java just nudge you a little bit to good and thought-out data structures, because dirty hacks like your 2xX string vector are not much easier to pull of.

So, what you'll have to think about in making your transition is how you think about your data and put this structure into a data structure. That is, you'd probably declare a WorkLoad class that consists of a list of WorkItem objects, each of which consists of a number of week days (or whatever is in your first array element, can't tell that from looking at your data) and a profit.

share|improve this answer
    
I was worried it might be something like that. It seems strange to have a bunch of different classes with very little purpose. I suppose I should get used to that with Java though. –  mowwwalker Oct 4 '11 at 7:27
1  
Actually, when those classes communicate intent, they have a lot of purpose even if using them means a few more lines of code. A pair of strings of numbers doesn't communicate intent at all. For small applications and one-man teams, that's not a big deal; for millions-of-lines-of-code applications worked on by large multinational teams, it can make a big difference. –  Michael Kjörling Oct 4 '11 at 13:54

I would go for something like

public class Main {
  // ...
  public static class Item {
     private List<Integer> list = new ArrayList<Integer>(); // good when you need add elements
     //or: private int[] array; // instantiate when you know number of olements
     private int something;
     // getters, maybe setters (or fill data in constructor and don't allow changing them
     // int that case, don't expose array/list, make accesor methods instead
  }

  public List<Item> yourFunction() {
       // something
  }
}  
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.