Asked  6 Months ago    Answers:  5   Viewed   26 times

I have the following code:

String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");
where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");

Those two appends are not compiling. How would that work correctly?

 Answers

67

The size of an array can't be modified. If you want a bigger array you have to instantiate a new one.

A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a ) gives you back your array if you need it in this form.

List<String> where = new ArrayList<String>();
where.add( ContactsContract.Contacts.HAS_PHONE_NUMBER+"=1" );
where.add( ContactsContract.Contacts.IN_VISIBLE_GROUP+"=1" );

If you need to convert it to a simple array...

String[] simpleArray = new String[ where.size() ];
where.toArray( simpleArray );

But most things you do with an array you can do with this ArrayList, too:

// iterate over the array
for( String oneItem : where ) {
    ...
}

// get specific items
where.get( 1 );
Tuesday, June 1, 2021
 
Bharanikumar
answered 6 Months ago
36

To start you off on your assignment, String.split splits strings on a regular expression and this expression may be an empty string:

String[] ary = "abc".split("");

Yields the array:

(java.lang.String[]) [, a, b, c]

Getting rid of the empty 1st entry is left as an exercise for the reader :-)

Note: In Java 8, the empty first element is no longer included.

Saturday, June 5, 2021
 
sassy_geekette
answered 6 Months ago
91

This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element.

If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262) at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Friday, June 18, 2021
 
elias
answered 6 Months ago
22

You need to use the new operator when creating the object

Contacts.add(new Data(name, address, contact)); // Creating a new object and adding it to list - single step

or else

Data objt = new Data(name, address, contact); // Creating a new object
Contacts.add(objt); // Adding it to the list

and your constructor shouldn't contain void. Else it becomes a method in your class.

public Data(String n, String a, String c) { // Constructor has the same name as the class and no return type as such
Tuesday, July 27, 2021
 
rasmusx
answered 4 Months ago
99

It would be nice like this:

public static void main (String [] args)
{
    double examMarks [] = {50,40,60,80,70,11};
    double courseworkmarks [] = {65,49,58,77,35,40};
    System.out.println ("These are the exam marks and the course work marks");//First row is the exam marks, second row is the course work marks
    computeMarks (examMarks);
    computeMarks1 (courseworkmarks);
    System.out.println ("These are the final marks");
    computeResults(examMarks, courseworkmarks);
}

public static void computeMarks(double[] examMarks)
{
    for (int row=0;row<examMarks.length;row++){
            System.out.print (examMarks[row] +"t");
        }
    System.out.println();
    }
public static void computeMarks1(double[] courseworkmarks)
{
    for (int row=0;row<courseworkmarks.length;row++){
            System.out.print (courseworkmarks[row] +"t");
        }
    System.out.println("n");
    }


public static void computeResults(double[] examMarks, double[] courseworkmarks)
{

        double avgMarks[] =new double[examMarks.length];

        for(int i=0;i<avgMarks.length;i++){
        int cwWeighting=40;
            avgMarks[i]=(examMarks[i]*(100-cwWeighting)+courseworkmarks[i]*cwWeighting)/100;

        System.out.print (avgMarks[i] +"t");
        }
}
Thursday, September 2, 2021
 
Ahmed Haque
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share