Java void进入一个int数组

时间:2015-11-30 07:52:24

标签: java

如何将void存储到数组中?

我的例子: count(String text)into int [] letter。

是否可以快速轻松地完成它或者我应该完全重写它?

我真的很感谢你的帮助

我的代码:

$(document).ready(function() {
    InitMap();
});
function LoadMap() {
    var locations = new Array(<?= implode(',',$gps);?>);
    var markers = new Array();
    var mapOptions = {
        center: new google.maps.LatLng(7.9597293,98.3333532),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    };

    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    $.each(locations, function(index, location) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(location[0], location[1]),
            map: map,
            icon: "http://<?=$_SERVER['HTTP_HOST']?>/images/marker-transparent.png"
        });

        var myOptions = {
            content: '<div class="infobox"><div class="image"><img src="http://html.realia.byaviators.com/assets/img/tmp/property-tiny-1.png" alt=""></div><div class="title"><a href="detail.html">1041 Fife Ave</a></div><div class="area"><span class="key">Area:</span><span class="value">200m<sup>2</sup></span></div><div class="price">€450 000.00</div><div class="link"><a href="detail.html">View more</a></div></div>',
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-146, -130),
            zIndex: null,
            closeBoxURL: "",
            infoBoxClearance: new google.maps.Size(1, 1),
            position: new google.maps.LatLng(location[0], location[1]),
            isHidden: false,
            pane: "floatPane",
            enableEventPropagation: false
        };
        marker.infobox = new InfoBox(myOptions);
        marker.infobox.isOpen = false;

        var myOptions = {
            draggable: true,
            content: '<div class="marker"><div class="marker-inner"></div></div>',
            disableAutoPan: true,
            pixelOffset: new google.maps.Size(-21, -15),
            position: new google.maps.LatLng(location[0], location[1]),
            closeBoxURL: "",
            isHidden: false,
            // pane: "mapPane",
            enableEventPropagation: true
        };
        marker.marker = new InfoBox(myOptions);
        marker.marker.open(map, marker);
        markers.push(marker);

        google.maps.event.addListener(marker, "click", function (e) {
            var curMarker = this;

            $.each(markers, function (index, marker) {
                // if marker is not the clicked marker, close the marker
                if (marker !== curMarker) {
                    marker.infobox.close();
                    marker.infobox.isOpen = false;
                }
            });


            if(curMarker.infobox.isOpen === false) {
                curMarker.infobox.open(map, this);
                curMarker.infobox.isOpen = true;
                map.panTo(curMarker.getPosition());
            } else {
                curMarker.infobox.close();
                curMarker.infobox.isOpen = false;
            }

        });
    });
}

function InitMap() {
    google.maps.event.addDomListener(window, 'load', LoadMap);
}

5 个答案:

答案 0 :(得分:1)

我可能错了,因为你的问题不是很清楚,但你想要做的似乎是:

public static int[] count(String text){
    int[] counts = new int[26];

    // iterate over each letter:
    for (char letter='A';letter<='Z';letter++){
        int currentIndex = (int) letter - 'A';

        // count the occurrences of the current letter:
        for (int i=0; i<text.length(); i++){
            if (letter==text.charAt(i) || (letter+32)==text.charAt(i)){
                counts[currentIndex]++;
            }
        } 
        // print the count for the current letter if non-zero
        if (counts[currentIndex]>0){
            System.out.println(letter+" = " + counts[currentIndex]);
        }
    }
    return counts;
}

肯定有一种更有效的方法可以做到这一点(迭代text中的字符并相应地递增计数索引会比检查text中所有字符26次更快),但这种方法的工作原理。

答案 1 :(得分:0)

我真的不明白你为什么要这样做,但你可以把int[]作为参数传递。

...
int[] count = new int[1];
count(text, count);
...

public static void count(String text, int[] count){
   ...
   count[0] = ..
}

答案 2 :(得分:0)

读取你的代码似乎你只想返回整数而不是整数数组(字符串中的字母数似乎)

public static int count(String text){
    int count;

    for (char letter=(char)65;letter<=90;letter++){
        count=0;     // are you sure you want to set the value of count to 0 every time you loop?
        for (int i=0; i<text.length(); i++){

            if (letter==text.charAt(i) || (letter+32)==text.charAt(i)){
            count++;
            }
        } 
        if (count>0){
            System.out.println(letter+" = "+count);
        }
    }
    return count;
}

在你的主要方法中写下:

String[] words = split(text);
int letter = count(text);

答案 3 :(得分:0)

    public int count()
{
count = 0;
for ...
...
...
return count;
}

你应该在函数末尾使用RETURN字,并在函数名旁边声明返回类型(int,string,...等)。

答案 4 :(得分:0)

你只需要旋转一次字符串:

public static int[] count(String text) {
    int[] totals = new int[26];
    for (char c : text.toUpperCase().toCharArray()) {
        int idx = (int)c - (int)'A';
        if ((idx >= 0) && (idx <= 26)) {
           totals[idx]++; 
        }
    }
    return totals;
}