开发者

Iterating through 2D array in Java

hello i have a issue and i want your help i have a table which is called citylink[10][2] and i want to make a check before i move on in my code if it's full to continue if it's not to break!!i know that i should use an if loop but i don't know what to put inside it!!

EDIT

for(int i=0; i < citylink.length; i++) {
  if(citylink[][]) {
    body=pF.fetchPage(cityli开发者_如何学Gonk[i][1]);
  }
}

i want first to check if in my table is full of data or at least the 5 first columns!!!and then insert in the body and use this command


You should consider using a java.util.List instead of arrays (Effective Java 2nd Edition, Item 25: Prefer lists to arrays). It looks like you're also using a 2-element array to represent a "city link"; this is not the best model for your data.

You should define a class CityLink, perhaps something like this:

public class CityLink {
   final City source;
   final City destination;
   //...
}

Then you declare a List<CityLink>.

API links

  • java.util.List<E>
    • int size() - Returns the number of elements in this list.
    • E get(int index) - Returns the element at the specified position in this list.
    • add(E e) - Appends the specified element to the end of this list
  • java.util.ArrayList<E>
    • Resizable-array implementation of the List interface.

On keeping a count of things

If you insist on using arrays, then you must keep a count of how many elements in the array are "real" elements. The easiest way to do this is to have an int count = 0; that you increment every time you add an element to the array.

At any given time, the only "real" elements in the array are arr[i] where i goes from 0 (inclusive) to count (exclusive). When count == arr.length; then the array is full and can no longer accommodate any additional elements.

Again, it needs to be said that doing this is a horrible way of solving your current problem, and will only lead to even more problems in the future. You really should be using a List.


On columns vs rows

or at least the 5 first columns!

Given this declaration:

int[][] table = new int[10][20];

Traditionally table is considered to have 10 rows, with 20 columns on each row.


If I understand you correctly, you want to make sure all links within your table are initialized, before you pass each of them to a method.

If you really want to use arrays, the code could be something like this:

for(int i=0; i < citylink.length; i++) {
  for(int j=0; j < citylink[i].length; j++) {
    if(citylink[i][j] == null) {
      citylink[i][j] = ...
    }
    body=pF.fetchPage(citylink[i][j]);
  }
}

But I agree with @poly in that Lists are preferable. The only compelling reason for using arrays could be backward compatibility with legacy code. Another case is if you want specific links associated to specific indexes within your collection; you can't do that easily with Lists, because a list can't have "holes". But then, you are probably better off with a Map.


You definitly want to avoid NullPointerException problems in your code, so you may want to add a check before calling fetchPage or you may want to add some extra code to the fetchPage method, which I'd prefer anyway.

for(int i=0; i < citylink.length; i++) {
  if(citylink[i][1] != null) {
    body=pF.fetchPage(citylink[i][1]);
  }
}

is the easiest solution to solve your problem. A more elegant solution is to implement a new method for checking a citylink row:

  //... inside some method
  for(int i=0; i < citylink.length; i++) {
    if(isValidCitylink(citylink[i])) {
      body=pF.fetchPage(citylink[i][1]);
    }
  }
  // ... more of this method
}

private boolean isValidCitylink(String[] citylink) {

  // check null or wrong format
  if (citylink == null || citylink.length != 2) return false;

  // check if both column contain a value
  if (citylink[0] == null || citylink[1] == null) return false

  return true;
}

This is what I'd suggest (taking your code from a previous question):

public String fetchPage(String url) {
    try {
        if (url != null) { // a null value check
          return URIUtil.encodeQuery(url);
        } else {
          return "No URL available";
        }
    } catch (URIException e) {
        e.printStackTrace();
    }
}

Please keep in mind, that in this code each assignment to body replaces its previous content and that body will contain the content of the last valid citylink URL of your list/array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜